2010-12-03 30 views
2

Disons que nous avons une extension PHP personnalisé comme:extensions PHP - appelez votre propre fonction PHP d'une autre fonction PHP

PHP_RSHUTDOWN_FUNCTION(myextension) 
{ 
    // How do I call myfunction() from here? 
    return SUCCESS; 
} 
PHP_FUNCTION(myfunction) 
{ 
    // Do something here 
    ... 
    RETURN_NULL; 
} 

Comment puis-je appeler myfunction() du gestionnaire de RSHUTDOWN?

+0

vous voulez exécuter? –

+0

oui, appelez == execute. – m1tk4

+0

Avez-vous déjà découvert cela? J'essayais de penser à un moyen d'appeler json_encode à l'intérieur d'un ext personnalisé. fonction et ne pouvait pas comprendre comment le retirer. – David

Répondre

5

Utilisation des macros fournies l'appel sera:

PHP_RSHUTDOWN_FUNCTION(myextension) 
{ 
    ZEND_FN(myFunction)(0, NULL, NULL, NULL, 0 TSRMLS_CC); 
    return SUCCESS; 
} 

Lorsque vous vous définir fonctionner comme PHP_FUNCTION(myFunction) le préprocesseur étendre vous êtes définition à:

ZEND_FN(myFunction)(INTERNAL_FUNCTION_PARAMETERS) 

qui est à son tour:

zif_myFunction(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC) 

Les macros de zend.h et php.h:

#define PHP_FUNCTION   ZEND_FUNCTION 
#define ZEND_FUNCTION(name)   ZEND_NAMED_FUNCTION(ZEND_FN(name)) 
#define ZEND_FN(name)      zif_##name 
#define ZEND_NAMED_FUNCTION(name)  void name(INTERNAL_FUNCTION_PARAMETERS) 
#define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC 
#define INTERNAL_FUNCTION_PARAM_PASSTHRU ht, return_value, return_value_ptr, this_ptr, return_value_used TSRMLS_CC 
2

Pourquoi ne pas vous faire le PHP_FUNCTION un talon comme ceci:

void doStuff() 
{ 
    // Do something here 
    ... 
} 

PHP_RSHUTDOWN_FUNCTION(myextension) 
{ 
    doStuff(); 
    return SUCCESS; 
} 
PHP_FUNCTION(myfunction) 
{ 
    doStuff(); 
    RETURN_NULL; 
}