diff options
author | JasperVanEck <jaspergkurtz@gmail.com> | 2013-11-28 15:46:02 +0100 |
---|---|---|
committer | JasperVanEck <jaspergkurtz@gmail.com> | 2013-11-28 15:46:02 +0100 |
commit | 9185842b1d2fd352f6d71a46d2017fa25cc3c6a7 (patch) | |
tree | 9d7af5cea5cbdd98dc53e48cd92d21080b8acea7 /Examples/FunctionWithParameters | |
parent | a6587568fe996abc6b128d5e78d61c342e95d922 (diff) |
Additional functions with parameters; references, objects and arrays
Diffstat (limited to 'Examples/FunctionWithParameters')
-rw-r--r-- | Examples/FunctionWithParameters/functionwithparameters.cpp | 62 | ||||
-rw-r--r-- | Examples/FunctionWithParameters/functionwithparameters.php | 70 |
2 files changed, 122 insertions, 10 deletions
diff --git a/Examples/FunctionWithParameters/functionwithparameters.cpp b/Examples/FunctionWithParameters/functionwithparameters.cpp index 01db0f6..4f70f26 100644 --- a/Examples/FunctionWithParameters/functionwithparameters.cpp +++ b/Examples/FunctionWithParameters/functionwithparameters.cpp @@ -6,9 +6,15 @@ */ /** - * Libraries used. + * Default Cpp libraries */ + +#include <string> #include <iostream> + +/** + * Our own library. + */ #include <phpcpp.h> /** @@ -43,6 +49,42 @@ Php::Value my_with_defined_parameters_function(Php::Parameters ¶ms) 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 + */ +void my_with_defined_parameters_reference_function(Php::Parameters ¶ms) +{ + params[0] = "I changed!"; +} + +/** + * 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; + } +} + +/** + * my_with_defined_object_parameters_function() + * The use of objects is not yet implemented. + * @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; + } +} + // Symbols are exported according to the "C" language extern "C" { @@ -52,14 +94,30 @@ extern "C" // create extension static Php::Extension extension("my_function_with_parameters","1.0"); - // add function to extension + // 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/FunctionWithParameters/functionwithparameters.php b/Examples/FunctionWithParameters/functionwithparameters.php index b1eff25..0ca360e 100644 --- a/Examples/FunctionWithParameters/functionwithparameters.php +++ b/Examples/FunctionWithParameters/functionwithparameters.php @@ -7,15 +7,69 @@ */ /* + * Test class. + */ +class MyPhpClass { + + public $aMemberVar = "aMemberVar"; + + public function __toString() + { + return "myPhpClass.__toString()"; + } + + public function getMemberVar() + { + return $aMemberVar; + } +} + +/* * Run a function with parameters. */ -echo 'A function which takes parameters, which are all undefined;' . - '\nmy_with_undefined_parameters_function("1st","2nd","3rd","4th")\n'; -echo my_with_undefined_parameters_function("1st","2nd","3rd","4th") . '\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'; +/* + * 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"; + +/* + * 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"; + +/* + * 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"; -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'; +echo my_with_defined_parameters_reference_function($referenceVar) . "\n"; + +echo "New value of referenceVar: " . $referenceVar ."\n\n\n"; + +/* + * A function which takes an array as a parameter + * my_with_defined_array_parameters_function($myArray) + */ +$myArray = array("a", "b", "c", "d"); +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); + +/* + * Accessing a non-existant parameters index will result in a segmentation fault. + * The segmentation fault will also occur when passing the wrong type of parameter. + */ |