diff options
-rw-r--r-- | Examples/CppClassesInPhp/30-phpcpp.ini | 2 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/Makefile | 2 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.cpp | 71 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.php | 13 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/functionwithparameters.cpp | 118 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/functionwithparameters.php | 49 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/includeMyCustomClass.h (renamed from Examples/CppClassesInPhp/includes.h) | 4 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/mycustomclass.cpp | 16 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/mycustomclass.h | 47 | ||||
-rw-r--r-- | Examples/simple/30-phpcpp.ini | 4 | ||||
-rw-r--r-- | Examples/simple/Makefile | 2 | ||||
-rw-r--r-- | Examples/simple/simple.cpp | 17 |
12 files changed, 95 insertions, 250 deletions
diff --git a/Examples/CppClassesInPhp/30-phpcpp.ini b/Examples/CppClassesInPhp/30-phpcpp.ini index 04961a9..8dcc005 100644 --- a/Examples/CppClassesInPhp/30-phpcpp.ini +++ b/Examples/CppClassesInPhp/30-phpcpp.ini @@ -1,4 +1,4 @@ ; configuration for phpcpp module ; priority=30 -extension=functionwithparameters.so +extension=cppclassinphp.so diff --git a/Examples/CppClassesInPhp/Makefile b/Examples/CppClassesInPhp/Makefile index 1a39c1e..fb549dc 100644 --- a/Examples/CppClassesInPhp/Makefile +++ b/Examples/CppClassesInPhp/Makefile @@ -9,7 +9,7 @@ PHP_CONFIG_DIR = /etc/php5/cli/conf.d LD = g++ LD_FLAGS = -Wall -shared -O2 -RESULT = functionwithparameters.so +RESULT = cppclassinphp.so PHPINIFILE = 30-phpcpp.ini diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp new file mode 100644 index 0000000..fffa655 --- /dev/null +++ b/Examples/CppClassesInPhp/cppclassinphp.cpp @@ -0,0 +1,71 @@ +/** + * cppclassinphp.cpp + * @author Jasper van Eck<jasper.vaneck@copernica.com> + * + * An example file to show the working of using a C++ class in PHP. + */ + +#include "includeMyCustomClass.h" +/** + * Namespace to use + */ +using namespace std; + +class MyCustomClass : public Php::Base +{ +private: + int _x; + +public: + MyCustomClass() + { + _x = 3; + std::cout << "MyCustomClass::MyCustomClass" << std::endl; + std::cout << _x << std::endl; + } + + virtual ~MyCustomClass() + { + std::cout << "MyCustomClass::~MyCustomClass" << std::endl; + } + + virtual void __construct() + { + std::cout << "MyCustomClass::__construct" << std::endl; + } + + virtual void __destruct() + { + std::cout << "MyCustomClass::__destruct" << std::endl; + } + + 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; + } +}; + + +// 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("my_function_with_parameters","1.0"); + + // add the custom class ot the extension + extension.add("MyClass", Php::Class<MyCustomClass>({ + Php::Public("myMethod", Php::Method<MyCustomClass>(&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 new file mode 100644 index 0000000..bea276c --- /dev/null +++ b/Examples/CppClassesInPhp/cppclassinphp.php @@ -0,0 +1,13 @@ +<?php +/** + * cppclassinphp.php + * @author Jasper van Eck<jasper.vaneck@copernica.com> + * + * 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 +$MyCustomClass = new MyClass(); + +// run a function of the class +$MyCustomClass->myMethod(1); diff --git a/Examples/CppClassesInPhp/functionwithparameters.cpp b/Examples/CppClassesInPhp/functionwithparameters.cpp deleted file mode 100644 index aa77ec1..0000000 --- a/Examples/CppClassesInPhp/functionwithparameters.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/** - * functionwithparameters.cpp - * @author Jasper van Eck<jasper.vaneck@copernica.com> - * - * An example file to show the working of a function call with parameters. - */ - -/** - * Default Cpp libraries - */ - -#include <string> -#include <iostream> - -/** - * Our own library. - */ -#include <phpcpp.h> - -/** - * Namespace to use - */ -using namespace std; - -/** - * 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; - } -} - -/** - * 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]; -} - -/** - * my_with_defined_array_parameters_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 << "Parameter " << i << ": " << params[0][i] << endl; - } -} - -/** - * 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!"; -} - -/** - * my_with_defined_object_parameters_function() - * @param Php::Parameters the given parameters - */ -void my_with_defined_object_parameters_function(Php::Parameters ¶ms) -{ - cout << params[0] << 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 - 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), - 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(); - } -} diff --git a/Examples/CppClassesInPhp/functionwithparameters.php b/Examples/CppClassesInPhp/functionwithparameters.php deleted file mode 100644 index 3b241d0..0000000 --- a/Examples/CppClassesInPhp/functionwithparameters.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php -/* - * functionwithparameters.php - * @author Jasper van Eck<jasper.vaneck@copernica.com> - * - * An example file to show the working of a function call with parameters, defined and undefined. - */ - -/* - * Test class. - */ -class MyPhpClass { - - public $aMemberVar = "aMemberVar"; - - public function __toString() - { - return "MyPhpClass.__toString()"; - } -} - -/* - * Run a function with parameters. - */ -echo "A function which takes parameters, which are all undefined;" . - "\n my_with_undefined_parameters_function('1st','2nd','3rd','4th')\n"; -echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n"; - -echo "A function which takes parameters, which are all defined; " . - "\nmy_with_defined_parameters_function(21,42)\n"; -echo my_with_defined_parameters_function(21,42) . "\n\n\n"; - -echo "A function which takes a reference of a parameter;" . - "\nmy_with_defined_parameters_reference_function(referenceVar)\n"; - -$referenceVar = "I am unchanged."; -echo "The value of referenceVar: " . $referenceVar. "\n"; - -echo my_with_defined_parameters_reference_function($referenceVar) . "\n"; - -echo "New value of referenceVar: " . $referenceVar ."\n\n\n"; - -$myPhpClass = new MyPhpClass; -echo "A function which takes an object as a parameter;" . - "\nmy_with_defined_object_parameter_function(myPhpClass)"; -echo my_with_defined_object_parameters_function($myPhpClass) . "\n\n\n"; - -echo "Accessing a non-existant parameters index will result in a segmentation fault.\n"; -echo "The segmentation fault will also occur when passing the wrong type of parameter.\n"; diff --git a/Examples/CppClassesInPhp/includes.h b/Examples/CppClassesInPhp/includeMyCustomClass.h index aa1e65a..c268a61 100644 --- a/Examples/CppClassesInPhp/includes.h +++ b/Examples/CppClassesInPhp/includeMyCustomClass.h @@ -1,7 +1,7 @@ /** * The includes.h */ - + /** * Default Cpp libraries */ @@ -22,4 +22,4 @@ using namespace std; /** * The test class. */ -#include "mycustomclass.h" +//#include "mycustomclass.h" diff --git a/Examples/CppClassesInPhp/mycustomclass.cpp b/Examples/CppClassesInPhp/mycustomclass.cpp deleted file mode 100644 index a2e3ca6..0000000 --- a/Examples/CppClassesInPhp/mycustomclass.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Includes - */ -#include "includes.h" - -/** - * Does some random printing. - * @param Php::Parameters - */ -void MyCustomClass::myMethod(Php::Parameters ¶ms) -{ - cout << "myMethod is called." << endl; - cout << "_x: " << _x << endl; - _x = params[0]; - cout << "New _x" << _x << endl; -} diff --git a/Examples/CppClassesInPhp/mycustomclass.h b/Examples/CppClassesInPhp/mycustomclass.h deleted file mode 100644 index 71054a0..0000000 --- a/Examples/CppClassesInPhp/mycustomclass.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * A test class for testing how to pass an object from php to CPP. - * - * - */ - - -/** - * Used libraries. - */ - -/** - * Namespace. - */ -using namespace std; - -class MyCustomClass : public Php::Base -{ -private: - int _x; - -public: - MyCustomClass() - { - _x = 3; - cout << "MyCustomClass::MyCustomClass" << endl; - cout << this << endl; - cout << _x << endl; - } - - virtual ~MyCustomClass() - { - cout << "MyCustomClass::~MyCustomClass" << endl; - } - - virtual void __construct() - { - cout << "MyCustomClass::__construct" << endl; - } - - virtual void __destruct() - { - cout << "MyCustomClass::__destruct" << endl; - } - - void myMethod(Php::Parameters ¶ms); -}; diff --git a/Examples/simple/30-phpcpp.ini b/Examples/simple/30-phpcpp.ini new file mode 100644 index 0000000..f366951 --- /dev/null +++ b/Examples/simple/30-phpcpp.ini @@ -0,0 +1,4 @@ +; configuration for phpcpp module +; priority=30 +extension=simple.so + diff --git a/Examples/simple/Makefile b/Examples/simple/Makefile index 6e3ecea..cfea8cf 100644 --- a/Examples/simple/Makefile +++ b/Examples/simple/Makefile @@ -9,7 +9,7 @@ PHP_CONFIG_DIR = /etc/php5/cli/conf.d LD = g++ LD_FLAGS = -Wall -shared -O2 -RESULT = functionnoparameters.so +RESULT = simple.so PHPINIFILE = 30-phpcpp.ini diff --git a/Examples/simple/simple.cpp b/Examples/simple/simple.cpp index 7dc9305..3a93bdc 100644 --- a/Examples/simple/simple.cpp +++ b/Examples/simple/simple.cpp @@ -74,17 +74,6 @@ Php::Value bubblesort(Php::Parameters ¶ms) } -void my_function() -{ - -} - -Php::Value my_integer_return() -{ - throw Php::Exception("er is iets mis"); - - return "abcd"; -} /** * Our own "my_plus" function that will be available in PHP @@ -173,7 +162,7 @@ extern "C" // define the functions extension.add("my_plus", my_plus, { Php::ByVal("a", Php::numericType), - Php::ByVal("b", Php::numericType) + Php::ByVal("b", Php::numericType), Php::ByVal("c", "MyClass"), Php::ByRef("d", Php::stringType) }); @@ -184,9 +173,7 @@ extern "C" // define classes extension.add("my_class", Php::Class<MyCustomClass>({ - Php::Public("a", 123), - Php::Protected("b", "abc"), - Php::Protected("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod)), + Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod)), Php::Public("__construct", Php::Method<MyCustomClass>(&MyCustomClass::__construct)) })); |