From 80c8a16fb145f7ed4867d31fad3c22318a1ce708 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 7 Dec 2013 13:12:25 -0800 Subject: replaces tabs in source code with regular spaces, added example for working with global variables --- Examples/CallPhpFunctions/callphpfunction.cpp | 48 ++-- Examples/CallPhpFunctions/callphpfunction.php | 30 +-- Examples/CppClassesInPhp/cppclassinphp.cpp | 44 ++-- Examples/CppClassesInPhp/cppclassinphp.php | 6 +- Examples/Exceptions/ExceptionCatch/exception.php | 10 +- .../Exceptions/ExceptionCatch/exceptionCatch.cpp | 38 ++-- Examples/Exceptions/ExceptionThrow/exception.php | 24 +- .../Exceptions/ExceptionThrow/exceptionThrow.cpp | 34 +-- Examples/Extension/extension.cpp | 22 +- Examples/Extension/extension.php | 6 +- .../FunctionNoParameters/functionnoparameters.cpp | 30 +-- .../FunctionNoParameters/functionnoparameters.php | 6 +- .../FunctionReturnValue/functionreturnvalue.cpp | 30 +-- .../FunctionReturnValue/functionreturnvalue.php | 8 +- Examples/FunctionVoid/functionvoid.cpp | 28 +-- Examples/FunctionVoid/functionvoid.php | 10 +- .../functionwithparameters.cpp | 130 +++++------ .../functionwithparameters.php | 28 +-- Examples/Globals/globals.cpp | 60 +++++ Examples/Globals/globals.php | 29 +++ Examples/README.md | 248 +++++++++++---------- 21 files changed, 485 insertions(+), 384 deletions(-) create mode 100644 Examples/Globals/globals.cpp create mode 100644 Examples/Globals/globals.php (limited to 'Examples') diff --git a/Examples/CallPhpFunctions/callphpfunction.cpp b/Examples/CallPhpFunctions/callphpfunction.cpp index 8f212f8..9932a3e 100644 --- a/Examples/CallPhpFunctions/callphpfunction.cpp +++ b/Examples/CallPhpFunctions/callphpfunction.cpp @@ -1,12 +1,12 @@ /** - * callphpfunction.cpp - * @author Jasper van Eck + * callphpfunction.cpp + * @author Jasper van Eck * - * An example file to show the working of a php function call in C++. + * An example file to show the working of a php function call in C++. */ /** - * Libraries used. + * Libraries used. */ #include #include @@ -17,37 +17,37 @@ using namespace std; /** - * call_php_function() - * Calls a function in PHP space. - * @param ¶ms - * @return Php::Value + * call_php_function() + * Calls a function in PHP space. + * @param ¶ms + * @return Php::Value */ Php::Value call_php_function(Php::Parameters ¶ms) { - // check whether the parameter is callable - if (!params[0].isCallable()) throw Php::Exception("Not a callable type."); - - // perform the callback - return params[0](1,2,3); + // check whether the parameter is callable + if (!params[0].isCallable()) throw Php::Exception("Not a callable type."); + + // perform the callback + return params[0](1,2,3); } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("call_php_function","1.0"); // add function to extension extension.add("call_php_function", call_php_function, { - Php::ByVal("addFunc", Php::callableType), - Php::ByVal("x", Php::numericType) - }); - - // return the extension module - return extension.module(); - } + Php::ByVal("addFunc", Php::callableType), + Php::ByVal("x", Php::numericType) + }); + + // return the extension module + return extension.module(); + } } diff --git a/Examples/CallPhpFunctions/callphpfunction.php b/Examples/CallPhpFunctions/callphpfunction.php index 77d633c..8da284d 100644 --- a/Examples/CallPhpFunctions/callphpfunction.php +++ b/Examples/CallPhpFunctions/callphpfunction.php @@ -1,33 +1,33 @@ + * callphpfunction.php + * @author Jasper van Eck * - * An example file to show the working of a php function call in C++. + * An example file to show the working of a php function call in C++. */ class MyClass { - function method($a,$b,$c) - { - return $a+$b+$c; - } + function method($a,$b,$c) + { + return $a+$b+$c; + } } function myFunction($a,$b,$c) { - return $a+$b+$c; + return $a+$b+$c; } /** - * Call a C++ function with a callable PHP function as its parameter. - * The PHP function is then executed from the C++ code. - * The PHP function is this case, adds three numbers. + * Call a C++ function with a callable PHP function as its parameter. + * The PHP function is then executed from the C++ code. + * The PHP function is this case, adds three numbers. */ echo(call_php_function(function($a, $b, $c){ - return $a + $b + $c; - })."\n"); - + return $a + $b + $c; + })."\n"); + echo(call_php_function("myFunction")."\n"); - + echo(call_php_function(array(new MyClass(), 'method'))."\n"); diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp index fffa655..2894fdc 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.cpp +++ b/Examples/CppClassesInPhp/cppclassinphp.cpp @@ -1,8 +1,8 @@ /** - * cppclassinphp.cpp - * @author Jasper van Eck + * cppclassinphp.cpp + * @author Jasper van Eck * - * An example file to show the working of using a C++ class in PHP. + * An example file to show the working of using a C++ class in PHP. */ #include "includeMyCustomClass.h" @@ -40,32 +40,32 @@ public: } void myMethod(Php::Parameters ¶ms) - { - std::cout << "myMethod is called." << std::endl; - std::cout << "_x: " << _x << std::endl; - _x = params[0]; - std::cout << "New _x" << _x << std::endl; - } + { + std::cout << "myMethod is called." << std::endl; + std::cout << "_x: " << _x << std::endl; + _x = params[0]; + std::cout << "New _x" << _x << std::endl; + } }; // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_function_with_parameters","1.0"); // add the custom class ot the extension - extension.add("MyClass", Php::Class({ - Php::Public("myMethod", Php::Method(&MyCustomClass::myMethod),{ - Php::ByVal("newX", Php::numericType) - }) - })); - - // return the extension module - return extension.module(); - } + extension.add("MyClass", Php::Class({ + Php::Public("myMethod", Php::Method(&MyCustomClass::myMethod),{ + Php::ByVal("newX", Php::numericType) + }) + })); + + // return the extension module + return extension.module(); + } } diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php index bea276c..1eccf95 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.php +++ b/Examples/CppClassesInPhp/cppclassinphp.php @@ -1,9 +1,9 @@ + * cppclassinphp.php + * @author Jasper van Eck * - * An example file to show the working of using a C++ class in PHP. + * An example file to show the working of using a C++ class in PHP. */ //create a MyCustomClass object, which is an object of a C++ class diff --git a/Examples/Exceptions/ExceptionCatch/exception.php b/Examples/Exceptions/ExceptionCatch/exception.php index 06bd534..0fec704 100644 --- a/Examples/Exceptions/ExceptionCatch/exception.php +++ b/Examples/Exceptions/ExceptionCatch/exception.php @@ -1,12 +1,12 @@ + * @author Jasper van Eck * - * This example shows the working of a C++ function that throws an - * exception, and that exception is then caught by PHP code. - * + * This example shows the working of a C++ function that throws an + * exception, and that exception is then caught by PHP code. + * */ // call the second C++ function that accepts a callback diff --git a/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp index 1b95277..a5f7b96 100644 --- a/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp +++ b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp @@ -1,14 +1,14 @@ /** - * exception.cpp - * @author Jasper van Eck + * exception.cpp + * @author Jasper van Eck * - * An example file to show the working of a C++ function that - * takes a callback function as parameter, and handles the - * exception thrown by the callback function. + * An example file to show the working of a C++ function that + * takes a callback function as parameter, and handles the + * exception thrown by the callback function. */ /** - * Libraries used. + * Libraries used. */ #include @@ -18,9 +18,9 @@ using namespace std; /** - * my_catch_exception_function() - * Catches the exception thrown by the PHP callback function. - * @param Php::Parameters + * my_catch_exception_function() + * Catches the exception thrown by the PHP callback function. + * @param Php::Parameters */ void my_catch_exception_function(Php::Parameters ¶ms) { @@ -47,18 +47,18 @@ void my_catch_exception_function(Php::Parameters ¶ms) // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_exception_catch","1.0"); // add function to extension extension.add("my_catch_exception_function", my_catch_exception_function, { - Php::ByVal("callback", Php::callableType); - }); - - // return the extension module - return extension.module(); - } + Php::ByVal("callback", Php::callableType); + }); + + // return the extension module + return extension.module(); + } } diff --git a/Examples/Exceptions/ExceptionThrow/exception.php b/Examples/Exceptions/ExceptionThrow/exception.php index 81bbd50..3afe86f 100644 --- a/Examples/Exceptions/ExceptionThrow/exception.php +++ b/Examples/Exceptions/ExceptionThrow/exception.php @@ -1,25 +1,25 @@ + * @author Jasper van Eck * - * This example shows the working of a C++ function that throws an - * exception, and that exception is then caught by PHP code. - * + * This example shows the working of a C++ function that throws an + * exception, and that exception is then caught by PHP code. + * */ // try-catch block to be able to handle the exception try { - // the my_throw_exception function is implemented in C++. and - // it is going to throw an exception - my_throw_exception_function(); + // the my_throw_exception function is implemented in C++. and + // it is going to throw an exception + my_throw_exception_function(); } catch (Exception $exception) { - // the exception object is thrown by C++ code, and caught by PHP - // code - echo $exception->getMessage(). "\n"; - + // the exception object is thrown by C++ code, and caught by PHP + // code + echo $exception->getMessage(). "\n"; + } diff --git a/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp index 3719e6f..d104047 100644 --- a/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp +++ b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp @@ -1,14 +1,14 @@ /** - * exception.cpp - * @author Jasper van Eck + * exception.cpp + * @author Jasper van Eck * - * An example file to show the working of a C++ function that - * throws an exception, which can be caught by PHP. - * + * An example file to show the working of a C++ function that + * throws an exception, which can be caught by PHP. + * */ /** - * Libraries used. + * Libraries used. */ #include @@ -18,28 +18,28 @@ using namespace std; /** - * my_throw_exception_function() - * Throws an exception which should be caught by PHP. + * my_throw_exception_function() + * Throws an exception which should be caught by PHP. */ void my_throw_exception_function() { - throw Php::Exception("I threw an exception in my_throw_exception_function()"); + throw Php::Exception("I threw an exception in my_throw_exception_function()"); } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_exception_throw","1.0"); // add function to extension extension.add("my_throw_exception_function", my_throw_exception_function); - - // return the extension module - return extension.module(); - } + + // return the extension module + return extension.module(); + } } diff --git a/Examples/Extension/extension.cpp b/Examples/Extension/extension.cpp index 9f7f495..99da5c7 100644 --- a/Examples/Extension/extension.cpp +++ b/Examples/Extension/extension.cpp @@ -1,8 +1,8 @@ /** - * extension.cpp - * @author Jasper van Eck + * extension.cpp + * @author Jasper van Eck * - * An example file to show the working of an extension. + * An example file to show the working of an extension. */ // library include #include @@ -15,13 +15,13 @@ using namespace std; // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_simple_extension","1.0"); - - // return the extension module - return extension.module(); - } + + // return the extension module + return extension.module(); + } } diff --git a/Examples/Extension/extension.php b/Examples/Extension/extension.php index 3941c8c..6f6b03c 100644 --- a/Examples/Extension/extension.php +++ b/Examples/Extension/extension.php @@ -1,9 +1,9 @@ + * extension.php + * @author Jasper van Eck * - * An example file to show the working of an extension. + * An example file to show the working of an extension. */ // print all the extensions currently loaded. diff --git a/Examples/FunctionNoParameters/functionnoparameters.cpp b/Examples/FunctionNoParameters/functionnoparameters.cpp index da645fd..8934169 100644 --- a/Examples/FunctionNoParameters/functionnoparameters.cpp +++ b/Examples/FunctionNoParameters/functionnoparameters.cpp @@ -1,12 +1,12 @@ /** - * functionnoparameters.cpp - * @author Jasper van Eck + * functionnoparameters.cpp + * @author Jasper van Eck * - * An example file to show the working of a function call without parameters. + * An example file to show the working of a function call without parameters. */ /** - * Libraries used. + * Libraries used. */ #include @@ -16,28 +16,28 @@ using namespace std; /** - * my_no_parameters_function() - * @return Php::Value + * my_no_parameters_function() + * @return Php::Value */ Php::Value my_no_parameters_function() { - return "42"; + return "42"; } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_function_return_value","1.0"); // add function to extension extension.add("my_no_parameters_function", my_no_parameters_function); - - // return the extension module - return extension.module(); - } + + // return the extension module + return extension.module(); + } } diff --git a/Examples/FunctionNoParameters/functionnoparameters.php b/Examples/FunctionNoParameters/functionnoparameters.php index f4035ae..9c62845 100644 --- a/Examples/FunctionNoParameters/functionnoparameters.php +++ b/Examples/FunctionNoParameters/functionnoparameters.php @@ -1,9 +1,9 @@ + * functionnoparameters.php + * @author Jasper van Eck * - * An example file to show the working of a function call without parameters. + * An example file to show the working of a function call without parameters. */ diff --git a/Examples/FunctionReturnValue/functionreturnvalue.cpp b/Examples/FunctionReturnValue/functionreturnvalue.cpp index 890a15f..a107fe7 100644 --- a/Examples/FunctionReturnValue/functionreturnvalue.cpp +++ b/Examples/FunctionReturnValue/functionreturnvalue.cpp @@ -1,12 +1,12 @@ /** - * functionreturnvalue.cpp - * @author Jasper van Eck + * functionreturnvalue.cpp + * @author Jasper van Eck * - * An example file to show the working of a function call with a return value. + * An example file to show the working of a function call with a return value. */ /** - * Libraries used. + * Libraries used. */ #include @@ -16,28 +16,28 @@ using namespace std; /** - * my_return_value_function() - * @return Php::Value + * my_return_value_function() + * @return Php::Value */ Php::Value my_return_value_function() { - return "42"; + return "42"; } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_function_return_value","1.0"); // add function to extension extension.add("my_return_value_function", my_return_value_function); - - // return the extension module - return extension.module(); - } + + // return the extension module + return extension.module(); + } } diff --git a/Examples/FunctionReturnValue/functionreturnvalue.php b/Examples/FunctionReturnValue/functionreturnvalue.php index 1626e19..dc3e68e 100644 --- a/Examples/FunctionReturnValue/functionreturnvalue.php +++ b/Examples/FunctionReturnValue/functionreturnvalue.php @@ -1,12 +1,12 @@ + * functionreturnvalue.php + * @author Jasper van Eck * - * An example file to show the working of a function call with a return value. + * An example file to show the working of a function call with a return value. */ /** - * Run a function which returns a value. + * Run a function which returns a value. */ echo my_return_value_function() . "\n"; diff --git a/Examples/FunctionVoid/functionvoid.cpp b/Examples/FunctionVoid/functionvoid.cpp index f4386ea..91b7bb3 100644 --- a/Examples/FunctionVoid/functionvoid.cpp +++ b/Examples/FunctionVoid/functionvoid.cpp @@ -1,12 +1,12 @@ /** - * functionvoid.cpp - * @author Jasper van Eck + * functionvoid.cpp + * @author Jasper van Eck * - * An example file to show the working of a void function call. + * An example file to show the working of a void function call. */ /** - * Libraries used. + * Libraries used. */ #include #include @@ -17,27 +17,27 @@ using namespace std; /** - * my_function_void() + * my_function_void() */ void my_function_void() { - cout << "In my_function_void()" << endl; + cout << "In my_function_void()" << endl; } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_function_void","1.0"); // add function to extension extension.add("my_void_function", my_function_void); - - // return the extension module - return extension.module(); - } + + // return the extension module + return extension.module(); + } } diff --git a/Examples/FunctionVoid/functionvoid.php b/Examples/FunctionVoid/functionvoid.php index e6fbb67..d057f92 100644 --- a/Examples/FunctionVoid/functionvoid.php +++ b/Examples/FunctionVoid/functionvoid.php @@ -1,13 +1,13 @@ + * functionvoid.php + * @author Jasper van Eck * - * An example file to show the working of a void function call. + * An example file to show the working of a void function call. */ /* - * Run the function with the given name. As you can see, the given name - * can be different from the actual function name. + * Run the function with the given name. As you can see, the given name + * can be different from the actual function name. */ my_void_function(); diff --git a/Examples/FunctionWithParameters/functionwithparameters.cpp b/Examples/FunctionWithParameters/functionwithparameters.cpp index 4f70f26..dd7bb9f 100644 --- a/Examples/FunctionWithParameters/functionwithparameters.cpp +++ b/Examples/FunctionWithParameters/functionwithparameters.cpp @@ -1,19 +1,19 @@ /** - * functionwithparameters.cpp - * @author Jasper van Eck + * functionwithparameters.cpp + * @author Jasper van Eck * - * An example file to show the working of a function call with parameters. + * An example file to show the working of a function call with parameters. */ /** - * Default Cpp libraries + * Default Cpp libraries */ #include #include /** - * Our own library. + * Our own library. */ #include @@ -23,102 +23,102 @@ using namespace std; /** - * my_with_undefined_parameters_function() - * @param Php:Parameters the given parameters + * my_with_undefined_parameters_function() + * @param Php:Parameters the given parameters */ void my_with_undefined_parameters_function(Php::Parameters ¶ms) { - for (unsigned int i = 0; i < params.size(); i++) - { - cout << "Parameter " << i << ": " << params[i] << endl; - } + for (unsigned int i = 0; i < params.size(); i++) + { + cout << "Parameter " << i << ": " << params[i] << endl; + } } /** - * my_with_defined_parameters_function() - * @param Php::Parameters the given parameters - * @return Php::Value Param[0] and Param[1] added + * my_with_defined_parameters_function() + * @param Php::Parameters the given parameters + * @return Php::Value Param[0] and Param[1] added */ Php::Value my_with_defined_parameters_function(Php::Parameters ¶ms) { - for (unsigned int i = 0; i < params.size(); i++) - { - cout << "Parameter " << i << ": " << params[i] << endl; - } - - return params[0] + params[1]; + for (unsigned int i = 0; i < params.size(); i++) + { + cout << "Parameter " << i << ": " << params[i] << endl; + } + + return params[0] + params[1]; } /** - * This functions receives a reference to a variable. When the variable is altered, - * so is the value in the php script. - * my_with_defined_parameters_reference_function() - * @param Php::Parameters the given parameters + * This functions receives a reference to a variable. When the variable is altered, + * so is the value in the php script. + * my_with_defined_parameters_reference_function() + * @param Php::Parameters the given parameters */ void my_with_defined_parameters_reference_function(Php::Parameters ¶ms) { - params[0] = "I changed!"; + params[0] = "I changed!"; } /** - * my_with_defined_parameters_reference_function() - * @param Php::Parameters the given parameters + * my_with_defined_parameters_reference_function() + * @param Php::Parameters the given parameters */ void my_with_defined_array_parameters_function(Php::Parameters ¶ms) { - for (int i = 0; i < params[0].size(); i++) - { - cout << "The array: " << params[0][i] << endl; - } + for (int i = 0; i < params[0].size(); i++) + { + cout << "The array: " << params[0][i] << endl; + } } /** - * my_with_defined_object_parameters_function() + * my_with_defined_object_parameters_function() * The use of objects is not yet implemented. - * @param Php::Parameters the given parameters + * @param Php::Parameters the given parameters */ void my_with_defined_object_parameters_function(Php::Parameters ¶ms) { - for (unsigned int i = 0; i < params.size(); i++) - { - cout << params[i] << endl; - } + for (unsigned int i = 0; i < params.size(); i++) + { + cout << params[i] << endl; + } } // Symbols are exported according to the "C" language extern "C" { - // export the "get_module" function that will be called by the Zend engine - PHPCPP_EXPORT void *get_module() - { - // create extension + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension static Php::Extension extension("my_function_with_parameters","1.0"); // add function, with undefined parameters, to extension extension.add("my_with_undefined_parameters_function", my_with_undefined_parameters_function); - - // add function, with defined numeric parameters, to extension - extension.add("my_with_defined_parameters_function", my_with_defined_parameters_function, { - Php::ByVal("x", Php::numericType), + + // add function, with defined numeric parameters, to extension + extension.add("my_with_defined_parameters_function", my_with_defined_parameters_function, { + Php::ByVal("x", Php::numericType), Php::ByVal("y", Php::numericType) - }); - - // add function, with defined parameter by reference, to extension - extension.add("my_with_defined_parameters_reference_function", my_with_defined_parameters_reference_function, { - Php::ByRef("string", Php::stringType) - }); - - // add function, with defined array parameter, to extension - extension.add("my_with_defined_array_parameters_function", my_with_defined_array_parameters_function, { - Php::ByVal("array", Php::arrayType) - }); - - // add function, with defined object parameter, to extension - extension.add("my_with_defined_object_parameters_function", my_with_defined_object_parameters_function, { - Php::ByVal("myClassObjVar", "MyPhpClass") - }); - - // return the extension module - return extension.module(); - } + }); + + // add function, with defined parameter by reference, to extension + extension.add("my_with_defined_parameters_reference_function", my_with_defined_parameters_reference_function, { + Php::ByRef("string", Php::stringType) + }); + + // add function, with defined array parameter, to extension + extension.add("my_with_defined_array_parameters_function", my_with_defined_array_parameters_function, { + Php::ByVal("array", Php::arrayType) + }); + + // add function, with defined object parameter, to extension + extension.add("my_with_defined_object_parameters_function", my_with_defined_object_parameters_function, { + Php::ByVal("myClassObjVar", "MyPhpClass") + }); + + // return the extension module + return extension.module(); + } } diff --git a/Examples/FunctionWithParameters/functionwithparameters.php b/Examples/FunctionWithParameters/functionwithparameters.php index 0ca360e..b1c3198 100644 --- a/Examples/FunctionWithParameters/functionwithparameters.php +++ b/Examples/FunctionWithParameters/functionwithparameters.php @@ -1,17 +1,17 @@ + * functionwithparameters.php + * @author Jasper van Eck * - * An example file to show the working of a function call with parameters, defined and undefined. + * An example file to show the working of a function call with parameters, defined and undefined. */ /* - * Test class. + * Test class. */ class MyPhpClass { - public $aMemberVar = "aMemberVar"; + public $aMemberVar = "aMemberVar"; public function __toString() { @@ -20,12 +20,12 @@ class MyPhpClass { public function getMemberVar() { - return $aMemberVar; - } + return $aMemberVar; + } } /* - * Run a function with parameters. + * Run a function with parameters. */ @@ -33,20 +33,20 @@ class MyPhpClass { * A function which takes parameters, which are all undefined; * my_with_undefined_parameters_function('1st','2nd','3rd','4th') */ -echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n"; +echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n"; /* * A function which takes parameters, which are all defined; * my_with_defined_parameters_function(21,42) */ - -echo my_with_defined_parameters_function(21,42) . "\n\n\n"; + +echo my_with_defined_parameters_function(21,42) . "\n\n\n"; /* * A function which takes a reference of a parameter * my_with_defined_parameters_reference_function(referenceVar) */ - + $referenceVar = "I am unchanged."; echo "The value of referenceVar: " . $referenceVar. "\n"; @@ -65,9 +65,9 @@ echo my_with_defined_array_parameters_function($myArray) . "\n\n\n"; * A function which takes an object as a parameter * my_with_defined_object_parameter_function(myPhpClass) */ - + $myPhpClass = new MyPhpClass; -echo my_with_defined_object_parameters_function($myPhpClass); +echo my_with_defined_object_parameters_function($myPhpClass); /* * Accessing a non-existant parameters index will result in a segmentation fault. diff --git a/Examples/Globals/globals.cpp b/Examples/Globals/globals.cpp new file mode 100644 index 0000000..6fa9eea --- /dev/null +++ b/Examples/Globals/globals.cpp @@ -0,0 +1,60 @@ +/** + * functionvoid.cpp + * @author Emiel Bruijntjes + * + * An example file to show how global variables can be accessed + */ + +/** + * Libraries used. + */ +#include +#include + +/** + * Namespace to use + */ +using namespace std; + +/** + * process_globals() + * + * This function reads and modifies global variables + */ +Php::Value process_globals() +{ + // all global variables can be accessed via the Php::globals variable, + // which is more or less the same as the PHP $_GLOBALS variable + + // set a global variable + Php::globals["a"] = 1; + + // increment a global variable + Php::globals["b"] += 1; + + // set a global variable to be an array + Php::globals["c"] = Php::Array(); + + // add a member to an array + Php::globals["c"]["member"] = 123; + + // if a global variable holds a function, we can call it + return Php::globals["d"](1,2,3); +} + +// Symbols are exported according to the "C" language +extern "C" +{ + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension + static Php::Extension extension("globals","1.0"); + + // add function to extension + extension.add("process_globals", process_globals); + + // return the extension module + return extension.module(); + } +} diff --git a/Examples/Globals/globals.php b/Examples/Globals/globals.php new file mode 100644 index 0000000..36febf6 --- /dev/null +++ b/Examples/Globals/globals.php @@ -0,0 +1,29 @@ + + * + * An example file to show the working of a void function call. + */ + +// we first need to set a number of globals +$b = 10; +$d = function($a,$b,$c) { + return $a+$b+$c; +}; + +// call the C++ function that will do something +$d = process_globals(); + +// the global variable $a should not have the value 1 +echo("a = $a\n"); + +// the variable $b should not be 11 +echo("b = $b\n"); + +// $c should be an array +echo("c['member'] = ".$c['member']."\n"); + +// $d is overwritten and now is 6 +echo("d = $d\n"); + diff --git a/Examples/README.md b/Examples/README.md index 5bd9e1e..d29673a 100644 --- a/Examples/README.md +++ b/Examples/README.md @@ -1,148 +1,160 @@ This directory contains a number of examples that show how to use the PHP-CPP library. -To run an example, there are several steps which need to be taken. +To run an example, there are a couple of steps that need to be taken. The first step is compiling and installing the PHPCPP library. This is -done running 'make' and then 'make install' in the main directory. -The second step is compiling the C++ code and made into an extension -usable by PHP. This is done by running 'make' and 'make install' -in an Example directory. Do make sure you've edited the Makefile -according to your own specific directories. +done by running 'make' and then 'make install' in the main directory. + +The second step is to compile the C++ code of an example and make it +into an extension usable by PHP. This is done by running 'make' and +'make install' in an Example directory. Do make sure you've edited the +Makefile according to your own specific directories. The following examples are available: ### [Extension](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Extension) - The first example does nothing - it only shows how to create your - own extension. This means your extension will be listed in the - output of "phpinfo()", and it is included in the array returned - by theget_loaded_modules() function. - - There are no functions or classes defined by this first example - extension. - - + The first example does nothing - it only shows how to create your + own extension. This means your extension will be listed in the + output of "phpinfo()", and it is included in the array returned + by theget_loaded_modules() function. + + There are no functions or classes defined by this first example + extension. + + ### [FunctionVoid](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionVoid) - This second example shows how to add a function to the extension - and call that function from the PHP code. Adding a function to - your extension means that you can call it anywhere from the PHP - code. - - Furthermore, it is possible to associate your C++ function with - another name. This other name is then used in PHP to call the C++ - function, rather than the original C++ function name. - - Functions and/or classes defined in this example. - - void my_function_void() Named as my_void_function() + This second example shows how to add a function to the extension + and call that function from the PHP code. Adding a function to + your extension means that you can call it anywhere from the PHP + code. + + Furthermore, it is possible to associate your C++ function with + another name. This other name is then used in PHP to call the C++ + function, rather than the original C++ function name. + + Functions and/or classes defined in this example. + - void my_function_void() Named as my_void_function() ### [FunctionReturnValue](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionReturnValue) - The third example shows how to return a value from C++ to PHP. - Virtually any type of value can be returned to PHP from C++. - The returned value must be returned as Php::Value, rather than - its own type. This Php::Value can then be used in your PHP code. - - Because a Php::Value is always returned, there is no need to specify - the return type of the function when adding it to your extension. - - Functions and/or classes defined in this example. - - Php::Value my_return_value_function() - + The third example shows how to return a value from C++ to PHP. + Virtually any type of value can be returned to PHP from C++. + The returned value must be returned as Php::Value object, rather + than a native C/C++ type. This Php::Value class takes care of + converting native values into values usable in your PHP code. + + Because a Php::Value is always returned, there is no need to specify + the return type of the function when adding it to your extension. + + Functions and/or classes defined in this example. + - Php::Value my_return_value_function() + ### [FunctionNoParameters](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionNoParameters) - The fourth example is a combination of the second and third example. - This example illustrates how to call a function without parameters. - The function is added to your extension, and can then be called from - your PHP script. - - The function returns a Php::Value to show that the call succeeded. - - Functions and/or classes defined in this example. - - Php::Value my_no_parameters_function() - - + The fourth example is a combination of the second and third example. + This example illustrates how to call a function without parameters. + The function is added to your extension, and can then be called from + your PHP script. + + The function returns a Php::Value to show that the call succeeded. + + Functions and/or classes defined in this example. + - Php::Value my_no_parameters_function() + + ### [FunctionWithParameters](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionWithParameters) - The fifth example is an example to show how several different types - of parameters can used for functions. There are two ways to pass a - parameter, by value(Php::ByVal) and by reference(Php::ByRef). Each - take two parameters of their own. The first being the parameter name, - and the second the parameter type. - - Furthermore, parameters are always stored in the Php::Parameters - object. This object is basicly an array which hold all the parameters, - in order. - - The first option being the undefined parameters. With undefined - parameters, we can pass any and as many parameters as we want to - the function. - - The second option is defining each parameter when adding the function - to your extension. In this case we have added two Php::numericType - parameters to the function. In 'type.h' you can find all avaiable - types, however not every type has been implemented yet. - - The third option is passing a reference of a variable. Meaning when - it is altered in the C++ code, its value will also change in the PHP - code. This can achieved by using Php:ByRef, rather than Php::ByVal. - - The fourth option is passing an array as parameter. The array - parameter will be accessible from the N-1 index of the - Php::Parameters object, where is the argument number of the array - when passing it to the function. - - The fifth and final option is passing an object. An object can be - passed in the same way as any other data type, except for that - you must specify what the class is of the object. This can be done - by passing a string with the class name as the second parameter to - Php::ByVal or Php::ByRef. - - Functions and/or classes defined in this example. - 1. void my_with_undefined_parameters_function(Php::Parameters ¶ms) - 2. Php::Value my_with_defined_parameters_function(Php::Parameters ¶ms) - 3. void my_with_defined_parameters_reference_function(Php::Parameters ¶ms) - 4. void my_with_defined_array_parameters_function(Php::Parameters ¶ms) - 5. void my_with_defined_object_parameters_function(Php::Parameters ¶ms) + The fifth example is an example to show how several different types + of parameters can used for functions. There are two ways to pass a + parameter, by value(Php::ByVal) and by reference(Php::ByRef). Each + take two parameters of their own. The first being the parameter name, + and the second the parameter type. + + Furthermore, parameters are always stored in the Php::Parameters + object. This object is basicly an array which hold all the parameters, + in order. + + The first option being the undefined parameters. With undefined + parameters, we can pass any and as many parameters as we want to + the function. + + The second option is defining each parameter when adding the function + to your extension. In this case we have added two Php::numericType + parameters to the function. In 'type.h' you can find all avaiable + types, however not every type has been implemented yet. + + The third option is passing a reference of a variable. Meaning when + it is altered in the C++ code, its value will also change in the PHP + code. This can achieved by using Php:ByRef, rather than Php::ByVal. + + The fourth option is passing an array as parameter. The array + parameter will be accessible from the N-1 index of the + Php::Parameters object, where is the argument number of the array + when passing it to the function. + + The fifth and final option is passing an object. An object can be + passed in the same way as any other data type, except for that + you must specify what the class is of the object. This can be done + by passing a string with the class name as the second parameter to + Php::ByVal or Php::ByRef. + + Functions and/or classes defined in this example. + 1. void my_with_undefined_parameters_function(Php::Parameters ¶ms) + 2. Php::Value my_with_defined_parameters_function(Php::Parameters ¶ms) + 3. void my_with_defined_parameters_reference_function(Php::Parameters ¶ms) + 4. void my_with_defined_array_parameters_function(Php::Parameters ¶ms) + 5. void my_with_defined_object_parameters_function(Php::Parameters ¶ms) + + +### [Globals](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Globals) + + Global PHP variables can be used accessed from your C++ code. You + can do this by accessing the Php::values array, which more or less + behaves the same as the $_GLOBALS array does in PHP. + + Functions and/or classes defined in this example. + 1. void process_globals() ### [Exceptions](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Exceptions) - The sixth example is composed of two parts, the throw exception and - the catch exception examples. The requirements of the catch example, - passing a callback as a parameter, have not yet been implemented. - - The throw example is there to show that an exception thrown in - a C++ function can be caught and handled in your PHP script. The - exception thrown is a Php::Exception. - - The catch example shows that when a PHP function is passed as a - callback, and is capable of throwing a (PHP) exception, that it - can be caught as Php::Exception and then handled in the C++ code. - However, the passing of a function as a callback has not yet been - implemented. It would need to be implemented for this specific - example to work. - - Functions and/or classes defined in this example. - - void my_catch_exception_function(Php::Parameters ¶ms) - - void my_throw_exception_function() + The sixth example is composed of two parts, the throw exception and + the catch exception examples. The requirements of the catch example, + passing a callback as a parameter, have not yet been implemented. + + The throw example is there to show that an exception thrown in + a C++ function can be caught and handled in your PHP script. The + exception thrown is a Php::Exception. + + The catch example shows that when a PHP function is passed as a + callback, and is capable of throwing a (PHP) exception, that it + can be caught as Php::Exception and then handled in the C++ code. + However, the passing of a function as a callback has not yet been + implemented. It would need to be implemented for this specific + example to work. + + Functions and/or classes defined in this example. + - void my_catch_exception_function(Php::Parameters ¶ms) + - void my_throw_exception_function() ### [PHP function calls](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/CallPhpFunctions) - The seventh example shows how to pass a callable PHP function as - a parameter. As can be seen in the example, there are several ways - of passing a PHP function to the C++ function. When a function is - passed, it is possible to use the () operator on the parameter, with - the correct amount of parameters for the callable PHP function. - - When using the wrong amount, or when trying to use the () operator - on a non-callable type, you will get PHP errors rather than - segmentation faults or other kinds of C++ errors. - - Functions and/or classes defined in this example. - - Php::Value call_php_function(Php::Parameters ¶ms) + The seventh example shows how to pass a callable PHP function as + a parameter. As can be seen in the example, there are several ways + of passing a PHP function to the C++ function. When a function is + passed, it is possible to use the () operator on the parameter, with + the correct amount of parameters for the callable PHP function. + + When using the wrong amount, or when trying to use the () operator + on a non-callable type, you will get PHP errors rather than + segmentation faults or other kinds of C++ errors. + + Functions and/or classes defined in this example. + - Php::Value call_php_function(Php::Parameters ¶ms) -- cgit v1.2.3