diff options
Diffstat (limited to 'Examples')
-rw-r--r-- | Examples/Makefile | 5 | ||||
-rw-r--r-- | Examples/simple/Makefile | 22 | ||||
-rw-r--r-- | Examples/simple/simple.cpp | 184 | ||||
-rw-r--r-- | Examples/simple/simple.php | 99 |
4 files changed, 310 insertions, 0 deletions
diff --git a/Examples/Makefile b/Examples/Makefile new file mode 100644 index 0000000..2f74b32 --- /dev/null +++ b/Examples/Makefile @@ -0,0 +1,5 @@ +all: + cd simple; $(MAKE) + +clean: + cd simple; $(MAKE) clean diff --git a/Examples/simple/Makefile b/Examples/simple/Makefile new file mode 100644 index 0000000..e60be3a --- /dev/null +++ b/Examples/simple/Makefile @@ -0,0 +1,22 @@ +CPP = g++ +RM = rm -f +CPP_FLAGS = -Wall -c -I. -O2 -std=c++11 + +LD = g++ +LD_FLAGS = -Wall -shared -O2 +RESULT = simple.so + +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} + diff --git a/Examples/simple/simple.cpp b/Examples/simple/simple.cpp new file mode 100644 index 0000000..7942a3c --- /dev/null +++ b/Examples/simple/simple.cpp @@ -0,0 +1,184 @@ +/** + * Simple.h + * + * A very simple extension that does almost nothing + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ +#include <string> +#include <iostream> +#include <phpcpp.h> + +/** + * Namespace to use + */ +using namespace std; + + +Php::Value bubblesort(Php::Parameters ¶ms) +{ + cout << "start bubblesort" << endl; + + // the array that was passed + Php::Value array(params[0]); + + cout << "go return" << endl; + + return array; + + // size of the array + int size = array.size(); + + cout << "convert to native" << endl; + + int *x = new int[size]; + for (int i=0; i<size; i++) x[i] = array[i]; + + cout << "converted" << endl; + + + // outer loop + for (int i=0; i<size; i++) + { + // left value + int left = x[i]; +// cout << "left: " << left << endl; + + // inner loop + for (int j=i+1; j<size; j++) + { + // right value + int right = x[j]; + + if (left < right) continue; + + // swap values + x[i] = right; + x[j] = left; + left = right; + } + } + + cout << "algorithm end" << endl; + + Php::Value r; + + for (int i=0; i<size; i++) r[i] = x[i]; + + + delete[] x; + + // done + return r; +} + +/** + * Our own "my_plus" function that will be available in PHP + * @param environment + * @param params + * @return Value + */ +static Php::Value my_plus(Php::Environment &env, Php::Parameters ¶ms) +{ + Php::Value r(0); + + for (unsigned int i=0; i<params.size(); i++) r += params[i]; + + return r; + + +// int p1 = params[0]; +// int p2 = params[1]; +// return p1 + p2; +// +// cout << "global g1: " << env["g1"].stringValue() << endl; +// cout << "global g2: " << env["g2"].stringValue() << endl; +// +// Php::Global g(env["g3"]); +// +// g = "zo kan het ook"; +// +// string output = env.call("strtoupper","test in lowercase"); +// +// cout << "output: " << output << endl; +// +// return p1 + p2; +} + +/** + * Custom class that will be available in PHP + */ +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) + { + cout << "myMethod GETS CALLED!!!!" << endl; + cout << this << endl; + cout << _x << endl; +// cout << "A: " << _properties["a"] << endl; +// cout << "Z: " << _properties["z"] << 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("simple","1.0"); + + // define the functions + extension.add("my_plus", my_plus, { +// Php::ByVal("a", Php::numericType), +// Php::ByVal("b", Php::numericType) +// Php::ByVal("c", "MyClass"), +// Php::ByRef("d", Php::stringType) + }); + + extension.add("bubblesort", bubblesort); + + Php::Method<MyCustomClass> x(&MyCustomClass::myMethod); + + // 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("__construct", Php::Method<MyCustomClass>(&MyCustomClass::__construct)) + })); + + // return the module entry + return extension.module(); + } +} + diff --git a/Examples/simple/simple.php b/Examples/simple/simple.php new file mode 100644 index 0000000..3abaa77 --- /dev/null +++ b/Examples/simple/simple.php @@ -0,0 +1,99 @@ +<?php + +//class XXX +//{ +// public function __toString() +// { +// return "MyClass"; +// } +//} +// +//$myvar = "hoi"; +// +//class MyClass { +// +// public function __toString() +// { +// return "aksjdfhsdfkj"; +// } +//} +// +//$g1 = 123; +//$g2 = "abc"; +// +//$result = my_plus(new MyClass(), array(), new MyClass(), $myvar, "blabla", new XXX()); +// +//echo("myvar = $myvar\n"); +// +//echo("resultaat: $result\n"); +// +//echo("g1: $g1\n"); +//echo("g2: $g2\n"); +//echo("g3: $g3\n"); + +function slowsort($input) +{ + $size = count($input); + for ($i=0; $i<$size; $i++) + { + $left = $input[$i]; + for ($j=$i+1; $j<$size; $j++) + { + $right = $input[$j]; + + if ($left < $right) continue; + + // swap values + $input[$i] = $right; + $input[$j] = $left; + $left = $right; + } + } + return $input; +} + +//if (class_exists("my_class")) echo("Warempel, de class bestaat\n"); +// +//class my_extended_class extends my_class +//{ +// public function myMethod($val) +// { +// echo($this->a."\n"); +// echo($this->b."\n"); +// echo("hoi\n"); +// +// parent::myMethod($val); +// } +// +//} +// +//$x = new my_extended_class(); +//$x->myMethod(123); + +$x = new my_class(); +$x->myMethod(); + + +//echo(my_plus(1,2,3,4)."\n"); + +$array = array(); +for ($i=0; $i<10000; $i++) $array[] = rand(); + +//$array = array(1,2,3); + +//print_r($array); +//bubblesort($array); + +//print_r($array); + + +//echo("my_class::a = ".$x->a."\n"); +//echo("my_class::b = ".$x->b."\n"); +// +//unset($x); +// +//echo("done\n"); + +//$x->my_method(); + + |