summaryrefslogtreecommitdiff
path: root/Examples/CppClassesInPhp
diff options
context:
space:
mode:
authorJasperVanEck <jaspergkurtz@gmail.com>2013-11-28 15:46:02 +0100
committerJasperVanEck <jaspergkurtz@gmail.com>2013-11-28 15:46:02 +0100
commit9185842b1d2fd352f6d71a46d2017fa25cc3c6a7 (patch)
tree9d7af5cea5cbdd98dc53e48cd92d21080b8acea7 /Examples/CppClassesInPhp
parenta6587568fe996abc6b128d5e78d61c342e95d922 (diff)
Additional functions with parameters; references, objects and arrays
Diffstat (limited to 'Examples/CppClassesInPhp')
-rw-r--r--Examples/CppClassesInPhp/30-phpcpp.ini4
-rw-r--r--Examples/CppClassesInPhp/Makefile32
-rw-r--r--Examples/CppClassesInPhp/functionwithparameters.cpp118
-rw-r--r--Examples/CppClassesInPhp/functionwithparameters.php49
-rw-r--r--Examples/CppClassesInPhp/includes.h25
-rw-r--r--Examples/CppClassesInPhp/mycustomclass.cpp16
-rw-r--r--Examples/CppClassesInPhp/mycustomclass.h47
7 files changed, 291 insertions, 0 deletions
diff --git a/Examples/CppClassesInPhp/30-phpcpp.ini b/Examples/CppClassesInPhp/30-phpcpp.ini
new file mode 100644
index 0000000..04961a9
--- /dev/null
+++ b/Examples/CppClassesInPhp/30-phpcpp.ini
@@ -0,0 +1,4 @@
+; configuration for phpcpp module
+; priority=30
+extension=functionwithparameters.so
+
diff --git a/Examples/CppClassesInPhp/Makefile b/Examples/CppClassesInPhp/Makefile
new file mode 100644
index 0000000..1a39c1e
--- /dev/null
+++ b/Examples/CppClassesInPhp/Makefile
@@ -0,0 +1,32 @@
+CPP = g++
+RM = rm -f
+CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
+
+PREFIX = /usr
+#Edit these lines to correspond with your own directories
+LIBRARY_DIR = ${PREFIX}/lib/php5/20121212
+PHP_CONFIG_DIR = /etc/php5/cli/conf.d
+
+LD = g++
+LD_FLAGS = -Wall -shared -O2
+RESULT = functionwithparameters.so
+
+PHPINIFILE = 30-phpcpp.ini
+
+SOURCES = $(wildcard *.cpp)
+OBJECTS = $(SOURCES:%.cpp=%.o)
+
+all: ${OBJECTS} ${RESULT}
+
+${RESULT}: ${OBJECTS}
+ ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
+
+clean:
+ ${RM} *.obj *~* ${OBJECTS} ${RESULT}
+
+${OBJECTS}:
+ ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
+
+install:
+ cp -f ${RESULT} ${LIBRARY_DIR}
+ cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}
diff --git a/Examples/CppClassesInPhp/functionwithparameters.cpp b/Examples/CppClassesInPhp/functionwithparameters.cpp
new file mode 100644
index 0000000..aa77ec1
--- /dev/null
+++ b/Examples/CppClassesInPhp/functionwithparameters.cpp
@@ -0,0 +1,118 @@
+/**
+ * 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 &params)
+{
+ 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 &params)
+{
+ 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 &params)
+{
+ 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 &params)
+{
+ 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 &params)
+{
+ 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
new file mode 100644
index 0000000..3b241d0
--- /dev/null
+++ b/Examples/CppClassesInPhp/functionwithparameters.php
@@ -0,0 +1,49 @@
+<?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/includes.h
new file mode 100644
index 0000000..aa1e65a
--- /dev/null
+++ b/Examples/CppClassesInPhp/includes.h
@@ -0,0 +1,25 @@
+/**
+ * The includes.h
+ */
+
+/**
+ * Default Cpp libraries
+ */
+
+#include <string>
+#include <iostream>
+
+/**
+ * Our own library.
+ */
+#include <phpcpp.h>
+
+/**
+ * Namespace to use
+ */
+using namespace std;
+
+/**
+ * The test class.
+ */
+#include "mycustomclass.h"
diff --git a/Examples/CppClassesInPhp/mycustomclass.cpp b/Examples/CppClassesInPhp/mycustomclass.cpp
new file mode 100644
index 0000000..a2e3ca6
--- /dev/null
+++ b/Examples/CppClassesInPhp/mycustomclass.cpp
@@ -0,0 +1,16 @@
+/**
+ * Includes
+ */
+#include "includes.h"
+
+/**
+ * Does some random printing.
+ * @param Php::Parameters
+ */
+void MyCustomClass::myMethod(Php::Parameters &params)
+{
+ 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
new file mode 100644
index 0000000..71054a0
--- /dev/null
+++ b/Examples/CppClassesInPhp/mycustomclass.h
@@ -0,0 +1,47 @@
+/**
+ * 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 &params);
+};