Project VersionsSearchPrevious topicNext topicThis Page |
Calling FunctionsΒΆCalling functions is another common action we do when create programs in PHP. Although many functions may be called directly using its internal function pointer in C, others can simply being called using the PHP userland. For example: //$length = strlen(some_variable);
PHALCON_INIT_VAR(length);
PHALCON_CALL_FUNC_PARAMS_1(length, "strlen", some_variable);
The macro PHALCON_CALL_FUNC_PARAMS_1 calls functions that requires 1 parameter returning a value. There are another macros to call functions in the PHP userland: //Calling substr() with its 3 arguments returning the substring into "part"
PHALCON_INIT_VAR(part);
PHALCON_CALL_FUNC_PARAMS_3(part, "substr", some_variable, start, length);
//Calling ob_start(), this function does not return anything
PHALCON_CALL_FUNC_NORETURN("ob_start");
//Closing a file with fclose
PHALCON_CALL_FUNC_PARAMS_1_NORETURN("fclose", file_handler);
As mentioned above, PHP already has many things going, the fact that an extension in C, does not mean we going to reinvent the wheel all over again develop. Also worth saying that we also try to avoid using very low-level features of C, if PHP already has its own version. PHP functions help us get a behavior similar to that programming in the PHP would. In this way we avoid possible errors and as result the framework works as if it were PHP. The following code opens a file in C and write something on it. Its functionality is limited because it only works on local files: FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile != NULL) {
fputs ("fopen example",pFile);
fclose (pFile);
}
Now write the same code using the PHP userland: PHALCON_INIT_VAR(mode);
ZVAL_STRING(mode, "w", 1);
PHALCON_INIT_VAR(file_handler);
PHALCON_CALL_FUNC_PARAMS_2(file_handler, "fopen", file_path, mode);
if (PHALCON_IS_NOT_FALSE(file_handler)) {
PHALCON_INIT_VAR(text);
ZVAL_STRING(text, "fopen example", 1);
PHALCON_CALL_FUNC_PARAMS_2_NORETURN("fputs", file_handler, text);
PHALCON_CALL_FUNC_PARAMS_1_NORETURN("fclose", file_handler);
}
The same code in PHP: <?php
$fp = fopen($file_path, "w");
if($fp){
fputs($fp, "fopen example");
fclose($fp);
}
|