├── .gitignore
├── .travis.yml
├── CMakeLists.txt
├── Examples
├── CallPhpFunctions
│ ├── 30-callphpfunction.ini
│ ├── Makefile
│ ├── callphpfunction.cpp
│ └── callphpfunction.php
├── ConstStaticProp
│ ├── cpp
│ │ ├── Makefile
│ │ ├── mytestext.cpp
│ │ ├── mytestext.h
│ │ └── mytestext.ini
│ ├── readme
│ └── test.php
├── CppClassesInPhp
│ ├── Makefile
│ ├── check_map.php
│ ├── cppclassinphp.cpp
│ ├── cppclassinphp.ini
│ ├── cppclassinphp.php
│ └── includeMyCustomClass.h
├── DlUnrestricted
│ ├── Makefile
│ ├── dlunrestricted.cpp
│ ├── dlunrestricted.ini
│ └── dlunrestricted.php
├── EmptyExtension
│ ├── Makefile
│ ├── main.cpp
│ └── yourextension.ini
├── Exceptions
│ ├── ExceptionCatch
│ │ ├── 30-exceptionCatch.ini
│ │ ├── Makefile
│ │ ├── exception.php
│ │ └── exceptionCatch.cpp
│ └── ExceptionThrow
│ │ ├── 30-exceptionThrow.ini
│ │ ├── Makefile
│ │ ├── exception.php
│ │ └── exceptionThrow.cpp
├── Extension
│ ├── 30-phpcpp.ini
│ ├── Makefile
│ ├── extension.cpp
│ └── extension.php
├── FunctionNoParameters
│ ├── 30-functionnoparameters.ini
│ ├── Makefile
│ ├── functionnoparameters.cpp
│ └── functionnoparameters.php
├── FunctionReturnValue
│ ├── 30-functionreturnvalue.ini
│ ├── Makefile
│ ├── functionreturnvalue.cpp
│ └── functionreturnvalue.php
├── FunctionVoid
│ ├── 30-functionvoid.ini
│ ├── Makefile
│ ├── functionvoid.cpp
│ └── functionvoid.php
├── FunctionWithParameters
│ ├── 30-functionwithparameters.ini
│ ├── Makefile
│ ├── functionwithparameters.cpp
│ └── functionwithparameters.php
├── Globals
│ ├── 30-globals.ini
│ ├── Makefile
│ ├── globals.cpp
│ └── globals.php
├── IncludeOnce
│ ├── 30-includeonce.ini
│ ├── Makefile
│ ├── class.php
│ ├── includeonce.cpp
│ └── includeonce.php
├── Makefile
├── README.md
├── ReturnObject
│ ├── Makefile
│ ├── child.h
│ ├── main.cpp
│ ├── master.h
│ ├── returnobject.ini
│ └── test.php
└── simple
│ ├── 30-phpcpp.ini
│ ├── Makefile
│ ├── simple.cpp
│ └── simple.php
├── LICENSE
├── Makefile
├── README.md
├── common
├── extensionbase.h
├── includes.h
├── modifiers.cpp
├── streambuf.cpp
└── streambuf.h
├── documentation
├── bubblesort.html
├── calling-functions-and-methods.html
├── classes-and-objects.html
├── constants.html
├── constructors-and-destructors.html
├── dynamic-loading.html
├── exceptions.html
├── extension-callbacks.html
├── functions.html
├── inheritance.html
├── ini.html
├── install.html
├── lambda-functions.html
├── loading-extensions.html
├── magic-interfaces.html
├── magic-methods.html
├── namespaces.html
├── output-and-errors.html
├── parameters.html
├── properties.html
├── special-features.html
├── ten-reasons-for-using-php-cpp.html
├── variables.html
└── your-first-extension.html
├── include
├── argument.h
├── array.h
├── arrayaccess.h
├── base.h
├── byref.h
├── byval.h
├── call.h
├── class.h
├── classbase.h
├── classtype.h
├── constant.h
├── countable.h
├── deprecated.h
├── error.h
├── exception.h
├── extension.h
├── file.h
├── function.h
├── global.h
├── globals.h
├── hashmember.h
├── hashparent.h
├── ini.h
├── inivalue.h
├── interface.h
├── iterator.h
├── message.h
├── modifiers.h
├── namespace.h
├── noexcept.h
├── object.h
├── parameters.h
├── platform.h
├── script.h
├── serializable.h
├── stream.h
├── streams.h
├── super.h
├── thread_local.h
├── throwable.h
├── traversable.h
├── type.h
├── value.h
├── valueiterator.h
├── version.h
├── visibility.h
├── zendcallable.h
└── zval.h
├── phpcpp.h
└── zend
├── arithmetic.h
├── base.cpp
├── boolmember.h
├── callable.cpp
├── callable.h
├── classbase.cpp
├── classimpl.cpp
├── classimpl.h
├── classtype.h
├── compileroptions.h
├── constant.cpp
├── constantfuncs.cpp
├── constantimpl.h
├── delayedfree.h
├── error.cpp
├── eval.cpp
├── exception.cpp
├── exception_handler.cpp
├── execarguments.h
├── executestate.h
├── exists.cpp
├── extension.cpp
├── extensionimpl.cpp
├── extensionimpl.h
├── extensionpath.h
├── file.cpp
├── floatmember.h
├── function.cpp
├── functor.cpp
├── functor.h
├── global.cpp
├── globals.cpp
├── hashiterator.h
├── hashmember.cpp
├── includes.h
├── ini.cpp
├── init.h
├── inivalue.cpp
├── interface.cpp
├── invaliditerator.h
├── iteratorimpl.cpp
├── iteratorimpl.h
├── lowercase.h
├── macros.h
├── member.h
├── members.cpp
├── method.h
├── module.cpp
├── module.h
├── namespace.cpp
├── nativefunction.h
├── notimplemented.h
├── nullmember.h
├── numericmember.h
├── object.cpp
├── objectimpl.h
├── opcodes.h
├── parametersimpl.h
├── property.h
├── rethrowable.h
├── sapi.cpp
├── script.cpp
├── state.h
├── stream.cpp
├── streambuf.cpp
├── streams.cpp
├── string.h
├── stringmember.h
├── super.cpp
├── symbol.h
├── throwable.cpp
├── traverseiterator.h
├── value.cpp
├── valueiterator.cpp
├── valueiteratorimpl.h
├── zendcallable.cpp
└── zval.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | *.d
2 | *.o
3 | *.a
4 | *.so
5 | *.txt
6 | *.a.*
7 | *.so.*
8 | build/
9 | .vscode/
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | # Although this is a C++ project, we use different PHP versions
2 | language: php
3 |
4 | # we need bionic
5 | dist: trusty
6 | sudo: required
7 | group: edge
8 |
9 | php:
10 | - 7.0
11 | - 7.1
12 | - 7.2
13 | - 7.3
14 | - 7.4
15 | - 8.0
16 | - nightly
17 |
18 |
19 | # setting the env is the easiest, because it will mix with all the separate php versions (travis does this)
20 | env:
21 | - COMPILER_PKG=g++-4.8 COMPILER=g++-4.8
22 | - COMPILER_PKG=g++-4.9 COMPILER=g++-4.9
23 | - COMPILER_PKG=g++-5 COMPILER=g++-5
24 | - COMPILER_PKG=g++-6 COMPILER=g++-6
25 | - COMPILER_PKG=g++-7 COMPILER=g++-7
26 | - COMPILER_PKG=g++-8 COMPILER=g++-8
27 | - COMPILER_PKG=clang++-4 COMPILER=clang++-4.0
28 | - COMPILER_PKG=clang++-5 COMPILER=clang++-5.0
29 | - COMPILER_PKG=clang++-6 COMPILER=clang++-6.0
30 |
31 | matrix:
32 | allow_failures:
33 | - php: nightly
34 |
35 | before_install:
36 | # install access to all gcc compilers
37 | - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
38 |
39 | # and all relevant clang compilers
40 | - sudo apt-add-repository "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-4.0 main"
41 | - sudo apt-add-repository "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main"
42 | - sudo apt-add-repository "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main"
43 |
44 | # update package index and install correct compiler
45 | - sudo apt-get -y update
46 | - sudo apt-get -y --allow-unauthenticated --no-install-recommends install ${COMPILER_PKG}
47 |
48 | script:
49 | # simply make the project with the set compiler
50 | - make -j4 COMPILER=$COMPILER && sudo make install
51 |
--------------------------------------------------------------------------------
/Examples/CallPhpFunctions/30-callphpfunction.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=callphpfunction.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/CallPhpFunctions/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 | PHP_CONFIG = $(shell which php-config)
5 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
6 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
7 |
8 | LD = g++
9 | LD_FLAGS = -Wall -shared -O2
10 | RESULT = callphpfunction.so
11 |
12 | PHPINIFILE = 30-callphpfunction.ini
13 |
14 | SOURCES = $(wildcard *.cpp)
15 | OBJECTS = $(SOURCES:%.cpp=%.o)
16 |
17 | all: ${OBJECTS} ${RESULT}
18 |
19 | ${RESULT}: ${OBJECTS}
20 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
21 |
22 | clean:
23 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
24 |
25 | ${OBJECTS}:
26 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
27 |
28 | install:
29 | cp -f ${RESULT} ${LIBRARY_DIR}/
30 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
31 |
32 | uninstall:
33 | rm ${LIBRARY_DIR}/${RESULT}
34 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
35 |
--------------------------------------------------------------------------------
/Examples/CallPhpFunctions/callphpfunction.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * callphpfunction.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a php function call in C++.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 | #include
13 |
14 | /**
15 | * Namespace to use
16 | */
17 | using namespace std;
18 |
19 | /**
20 | * call_php_function()
21 | * Calls a function in PHP space.
22 | * @param ¶ms
23 | * @return Php::Value
24 | */
25 | Php::Value call_php_function(Php::Parameters ¶ms)
26 | {
27 | // check whether the parameter is callable
28 | if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
29 |
30 | // perform the callback
31 | return params[0](1,2,3);
32 | }
33 |
34 |
35 | // Symbols are exported according to the "C" language
36 | extern "C"
37 | {
38 | // export the "get_module" function that will be called by the Zend engine
39 | PHPCPP_EXPORT void *get_module()
40 | {
41 | // create extension
42 | static Php::Extension extension("call_php_function","1.0");
43 |
44 | // add function to extension
45 | extension.add("call_php_function", {
46 | Php::ByVal("addFunc", Php::Type::Callable),
47 | Php::ByVal("x", Php::Type::Numeric)
48 | });
49 |
50 | // return the extension module
51 | return extension.module();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Examples/CallPhpFunctions/callphpfunction.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a php function call in C++.
7 | */
8 |
9 | class MyClass
10 | {
11 | function method($a,$b,$c)
12 | {
13 | return $a+$b+$c;
14 | }
15 | }
16 |
17 | function myFunction($a,$b,$c)
18 | {
19 | return $a+$b+$c;
20 | }
21 |
22 | /**
23 | * Call a C++ function with a callable PHP function as its parameter.
24 | * The PHP function is then executed from the C++ code.
25 | * The PHP function is this case, adds three numbers.
26 | */
27 | echo(call_php_function(function($a, $b, $c){
28 | return $a + $b + $c;
29 | })."\n");
30 |
31 | echo(call_php_function("myFunction")."\n");
32 |
33 | echo(call_php_function(array(new MyClass(), 'method'))."\n");
34 |
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/cpp/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = mytestext.so
12 |
13 | PHPINIFILE = mytestext.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/cpp/mytestext.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * cppclassinphp.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of using a C++ class in PHP.
6 | */
7 |
8 | #include "mytestext.h"
9 | /**
10 | * Namespace to use
11 | */
12 | using namespace std;
13 |
14 | class MyTestExt : public Php::Base
15 | {
16 |
17 | public:
18 | MyTestExt() {}
19 |
20 | virtual ~MyTestExt() {}
21 |
22 | virtual void __construct() {}
23 |
24 | };
25 |
26 |
27 |
28 | // Symbols are exported according to the "C" language
29 | extern "C"
30 | {
31 | // export the "get_module" function that will be called by the Zend engine
32 | PHPCPP_EXPORT void *get_module()
33 | {
34 | // create extension
35 | static Php::Extension extension("my_test_ext","0.1a");
36 |
37 | // add the custom class ot the extension
38 | extension.add(
39 | "MyTestClass",
40 | Php::Class({
41 |
42 | // Private PHP constructor! You can't instance object of MyTestClass
43 | Php::Private("__construct", Php::Method(&MyTestExt::__construct)),
44 |
45 | Php::Const("version", "v0.01-alpha"),
46 | Php::Const("PI", 3.14159265),
47 | Php::Const("IMISNULL"),
48 | })
49 | );
50 |
51 | // return the extension module
52 | return extension.module();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/cpp/mytestext.h:
--------------------------------------------------------------------------------
1 | /**
2 | * The includes.h
3 | */
4 |
5 | /**
6 | * Default Cpp libraries
7 | */
8 |
9 | #include
10 | #include
11 |
12 | /**
13 | * Our own library.
14 | */
15 | #include
16 |
17 | /**
18 | * Namespace to use
19 | */
20 | using namespace std;
21 |
22 | /**
23 | * The test class.
24 | */
25 | //#include "mycustomclass.h"
26 |
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/cpp/mytestext.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=mytestext.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/readme:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CopernicaMarketingSoftware/PHP-CPP/ad90b5a16e108f16a51afac3a4d92f6d72173db8/Examples/ConstStaticProp/readme
--------------------------------------------------------------------------------
/Examples/ConstStaticProp/test.php:
--------------------------------------------------------------------------------
1 | $offset = $value;
41 | }
42 | }
43 | public function offsetExists($offset) {
44 | return isset($this->$offset);
45 | }
46 | public function offsetUnset($offset) {
47 | unset($this->$offset);
48 | }
49 | public function offsetGet($offset) {
50 | return isset($this->$offset) ? $this->$offset : null;
51 | }
52 |
53 | }
54 |
55 | class emptyClass {}
56 |
57 | $arr = array(
58 | 'qwe' => 'qweqweqweqw',
59 | 'asd' => 'Привет!', // check UTF-8 chars
60 | 'zxccvx' => 'sdfsecvyh6bug6yfty',
61 | 1=>2,
62 | '2'=>2,
63 | 44,
64 | new cl1(),
65 | '%'=>'%$%$%',
66 | );
67 |
68 | $arr = new cl1();
69 |
70 | $arr[5] = 55;
71 | $arr['strstr'] = 'strstrstrstrstrstr';
72 |
73 | //$arr = new emptyClass();
74 | //$arr = array();
75 |
76 |
77 | $q = new MyClass();
78 |
79 | var_export($arr);
80 |
81 | //$q->loopArray($arr);
82 |
83 | // Works for objects and arrays
84 | $q->loopObject($arr);
85 | //$q->loopObject($arr);
86 |
87 |
--------------------------------------------------------------------------------
/Examples/CppClassesInPhp/cppclassinphp.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=cppclassinphp.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/CppClassesInPhp/cppclassinphp.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of using a C++ class in PHP.
7 | */
8 |
9 | class TestClass
10 | {
11 | public $x = 1223;
12 |
13 | public function __construct()
14 | {
15 | echo("TestClass::__construct\n");
16 | }
17 |
18 | }
19 |
20 | //create a MyCustomClass object, which is an object of a C++ class
21 | $object1 = new MyClass();
22 |
23 |
24 | $object2 = $object1->myMethod(1);
25 | $object2->myMethod(1);
26 |
27 | echo("unset\n");
28 |
29 | unset($object1);
30 |
31 | echo("got here\n");
32 |
33 | return;
34 |
35 | //echo("prop x: ".$object1->x."\n");
36 |
37 | $object1->x = 10;
38 | $object1->y = 20;
39 |
40 | echo("prop x: ".$object1->x."\n");
41 | echo("prop y: ".$object1->y."\n");
42 |
43 | $object2 = clone $object1;
44 |
45 | echo("prop x: ".$object2->x."\n");
46 | echo("prop y: ".$object2->y."\n");
47 |
48 |
49 | return;
50 |
51 | // run a function of the class
52 | $obj = $object->myMethod("MyClass");
53 |
54 | echo(get_class($obj)."\n");
55 | //echo($obj->format("Y-m-d")."\n");
56 |
57 | //echo($obj->x."\n");
58 |
59 | return;
60 |
61 | //$object->myMethod(2);
62 |
63 | echo($object->property1."\n");
64 | echo($object->property2."\n");
65 |
--------------------------------------------------------------------------------
/Examples/CppClassesInPhp/includeMyCustomClass.h:
--------------------------------------------------------------------------------
1 | /**
2 | * The includes.h
3 | */
4 |
5 | /**
6 | * Default Cpp libraries
7 | */
8 |
9 | #include
10 | #include
11 |
12 | /**
13 | * Our own library.
14 | */
15 | #include
16 |
17 | /**
18 | * Namespace to use
19 | */
20 | using namespace std;
21 |
22 | /**
23 | * The test class.
24 | */
25 | //#include "mycustomclass.h"
26 |
--------------------------------------------------------------------------------
/Examples/DlUnrestricted/dlunrestricted.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * functionvoid.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a void function call.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 |
13 | /**
14 | * Function to load every possible extension by pathname
15 | *
16 | * It takes one argument: the filename of the PHP extension, and returns a
17 | * boolean to indicate whether the extension was correctly loaded.
18 | *
19 | * This function goes further than the original PHP dl() fuction, because
20 | * it does not check whether the passed in extension object is stored in the
21 | * right directory. Literally every possible extension, also local ones
22 | * created by end users, can be loaded.
23 | *
24 | * @param params Vector of parameters
25 | * @return boolean
26 | */
27 | Php::Value dl_unrestricted(Php::Parameters ¶ms)
28 | {
29 | // get extension name
30 | std::string pathname = params[0];
31 |
32 | // should it be opened persistently?
33 | bool persistent = params.size() > 1 ? params[1].boolValue() : false;
34 |
35 | // load the extension
36 | return Php::dl(pathname, persistent);
37 | }
38 |
39 | /**
40 | * Switch to C context to ensure that the get_module() function
41 | * is callable by C programs (which the Zend engine is)
42 | */
43 | extern "C" {
44 | /**
45 | * Startup function that is called by the Zend engine
46 | * to retrieve all information about the extension
47 | * @return void*
48 | */
49 | PHPCPP_EXPORT void *get_module() {
50 | // create static instance of the extension object
51 | static Php::Extension myExtension("dl_unrestricted", "1.0");
52 |
53 | // the extension has one method
54 | myExtension.add("dl_unrestricted", {
55 | Php::ByVal("pathname", Php::Type::String)
56 | });
57 |
58 | // return the extension
59 | return myExtension;
60 | }
61 | }
62 |
63 |
--------------------------------------------------------------------------------
/Examples/DlUnrestricted/dlunrestricted.ini:
--------------------------------------------------------------------------------
1 | ; configuration for dlunrestriced module
2 | extension=dlunrestricted.so
3 |
4 |
--------------------------------------------------------------------------------
/Examples/DlUnrestricted/dlunrestricted.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a void function call.
7 | */
8 |
9 | /*
10 | * Run the function with the given name. As you can see, the given name
11 | * can be different from the actual function name.
12 | */
13 | my_void_function();
14 |
--------------------------------------------------------------------------------
/Examples/EmptyExtension/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | /**
4 | * tell the compiler that the get_module is a pure C function
5 | */
6 | extern "C" {
7 |
8 | /**
9 | * Function that is called by PHP right after the PHP process
10 | * has started, and that returns an address of an internal PHP
11 | * strucure with all the details and features of your extension
12 | *
13 | * @return void* a pointer to an address that is understood by PHP
14 | */
15 | PHPCPP_EXPORT void *get_module()
16 | {
17 | // static(!) Php::Extension object that should stay in memory
18 | // for the entire duration of the process (that's why it's static)
19 | static Php::Extension extension("yourextension", "1.0");
20 |
21 | // @todo add your own functions, classes, namespaces to the extension
22 |
23 | // return the extension
24 | return extension;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Examples/EmptyExtension/yourextension.ini:
--------------------------------------------------------------------------------
1 | extension=yourextension.so
2 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionCatch/30-exceptionCatch.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=exceptionCatch.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionCatch/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = exceptionCatch.so
12 |
13 | PHPINIFILE = 30-exceptionCatch.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionCatch/exception.php:
--------------------------------------------------------------------------------
1 |
6 | *
7 | * This example shows the working of a C++ function that throws an
8 | * exception, and that exception is then caught by PHP code.
9 | *
10 | */
11 |
12 | // call the second C++ function that accepts a callback
13 | my_catch_exception_function(function($a, $b, $c) {
14 |
15 | // throw an exception from PHP - the C++ code will catch this exception
16 | throw new Exception("Example exception");
17 | });
18 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * exception.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a C++ function that
6 | * takes a callback function as parameter, and handles the
7 | * exception thrown by the callback function.
8 | */
9 |
10 | /**
11 | * Libraries used.
12 | */
13 | #include
14 | #include
15 |
16 | /**
17 | * Namespace to use
18 | */
19 | using namespace std;
20 |
21 | /**
22 | * my_catch_exception_function()
23 | * Catches the exception thrown by the PHP callback function.
24 | * @param Php::Parameters
25 | */
26 | void my_catch_exception_function(Php::Parameters ¶ms)
27 | {
28 | // the first parameter should be a callback
29 | Php::Value callback = params[0];
30 |
31 | // this must be a callable type
32 | if (!callback.isCallable()) throw Php::Exception("Parameter 0 is not a function");
33 |
34 | // we're going to call a function that could throw an exception, start a
35 | // try-catch block to deal with that
36 | try
37 | {
38 | // call the function
39 | callback("some","example","parameters");
40 | }
41 | catch (Php::Exception &exception)
42 | {
43 | // handle the exception that was thrown from PHP space
44 | std::cout << "exception caught in CPP code" << std::endl;
45 | }
46 | }
47 |
48 |
49 | // Symbols are exported according to the "C" language
50 | extern "C"
51 | {
52 | // export the "get_module" function that will be called by the Zend engine
53 | PHPCPP_EXPORT void *get_module()
54 | {
55 | // create extension
56 | static Php::Extension extension("my_exception_catch","1.0");
57 |
58 | // add function to extension
59 | extension.add("my_catch_exception_function");
60 |
61 | // return the extension module
62 | return extension.module();
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionThrow/30-exceptionThrow.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=exceptionThrow.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionThrow/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = exceptionThrow.so
12 |
13 | PHPINIFILE = 30-exceptionThrow.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionThrow/exception.php:
--------------------------------------------------------------------------------
1 |
6 | *
7 | * This example shows the working of a C++ function that throws an
8 | * exception, and that exception is then caught by PHP code.
9 | *
10 | */
11 |
12 | // try-catch block to be able to handle the exception
13 | try
14 | {
15 | // the my_throw_exception function is implemented in C++. and
16 | // it is going to throw an exception
17 | my_throw_exception_function();
18 | }
19 | catch (Exception $exception)
20 | {
21 | // the exception object is thrown by C++ code, and caught by PHP
22 | // code
23 | echo $exception->getMessage(). "\n";
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * exception.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a C++ function that
6 | * throws an exception, which can be caught by PHP.
7 | *
8 | */
9 |
10 | /**
11 | * Libraries used.
12 | */
13 | #include
14 |
15 | /**
16 | * Namespace to use
17 | */
18 | using namespace std;
19 |
20 | /**
21 | * my_throw_exception_function()
22 | * Throws an exception which should be caught by PHP.
23 | */
24 | void my_throw_exception_function()
25 | {
26 | throw Php::Exception("I threw an exception in my_throw_exception_function()");
27 | }
28 |
29 |
30 | // Symbols are exported according to the "C" language
31 | extern "C"
32 | {
33 | // export the "get_module" function that will be called by the Zend engine
34 | PHPCPP_EXPORT void *get_module()
35 | {
36 | // create extension
37 | static Php::Extension extension("my_exception_throw","1.0");
38 |
39 | // add function to extension
40 | extension.add("my_throw_exception_function");
41 |
42 | // return the extension module
43 | return extension.module();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Examples/Extension/30-phpcpp.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=extension.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/Extension/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = extension.so
12 |
13 | PHPINIFILE = 30-phpcpp.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/Extension/extension.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * extension.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of an extension.
6 | */
7 | // library include
8 | #include
9 |
10 | /**
11 | * Namespace to use
12 | */
13 | using namespace std;
14 |
15 | // Symbols are exported according to the "C" language
16 | extern "C"
17 | {
18 | // export the "get_module" function that will be called by the Zend engine
19 | PHPCPP_EXPORT void *get_module()
20 | {
21 | // create extension
22 | static Php::Extension extension("my_simple_extension","1.0");
23 |
24 | // return the extension module
25 | return extension.module();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Examples/Extension/extension.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of an extension.
7 | */
8 |
9 | // print all the extensions currently loaded.
10 | print_r(get_loaded_extensions());
11 |
--------------------------------------------------------------------------------
/Examples/FunctionNoParameters/30-functionnoparameters.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=functionnoparameters.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/FunctionNoParameters/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = functionnoparameters.so
12 |
13 | PHPINIFILE = 30-functionnoparameters.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
--------------------------------------------------------------------------------
/Examples/FunctionNoParameters/functionnoparameters.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * functionnoparameters.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a function call without parameters.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 |
13 | /**
14 | * Namespace to use
15 | */
16 | using namespace std;
17 |
18 | /**
19 | * my_no_parameters_function()
20 | * @return Php::Value
21 | */
22 | Php::Value my_no_parameters_function()
23 | {
24 | return "42";
25 | }
26 |
27 |
28 | // Symbols are exported according to the "C" language
29 | extern "C"
30 | {
31 | // export the "get_module" function that will be called by the Zend engine
32 | PHPCPP_EXPORT void *get_module()
33 | {
34 | // create extension
35 | static Php::Extension extension("my_function_no_parameters","1.0");
36 |
37 | // add function to extension
38 | extension.add("my_no_parameters_function");
39 |
40 | // return the extension module
41 | return extension.module();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Examples/FunctionNoParameters/functionnoparameters.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a function call without parameters.
7 | */
8 |
9 |
10 | //Run a function without parameters.
11 | echo my_no_parameters_function() . "\n";
12 |
--------------------------------------------------------------------------------
/Examples/FunctionReturnValue/30-functionreturnvalue.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=functionreturnvalue.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/FunctionReturnValue/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = functionreturnvalue.so
12 |
13 | PHPINIFILE = 30-functionreturnvalue.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/FunctionReturnValue/functionreturnvalue.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * functionreturnvalue.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a function call with a return value.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 |
13 | /**
14 | * Namespace to use
15 | */
16 | using namespace std;
17 |
18 | /**
19 | * my_return_value_function()
20 | * @return Php::Value
21 | */
22 | Php::Value my_return_value_function()
23 | {
24 | return "42";
25 | }
26 |
27 |
28 | // Symbols are exported according to the "C" language
29 | extern "C"
30 | {
31 | // export the "get_module" function that will be called by the Zend engine
32 | PHPCPP_EXPORT void *get_module()
33 | {
34 | // create extension
35 | static Php::Extension extension("my_function_return_value","1.0");
36 |
37 | // add function to extension
38 | extension.add("my_return_value_function");
39 |
40 | // return the extension module
41 | return extension.module();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Examples/FunctionReturnValue/functionreturnvalue.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a function call with a return value.
7 | */
8 |
9 | /**
10 | * Run a function which returns a value.
11 | */
12 | echo my_return_value_function() . "\n";
13 |
--------------------------------------------------------------------------------
/Examples/FunctionVoid/30-functionvoid.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=functionvoid.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/FunctionVoid/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = functionvoid.so
12 |
13 | PHPINIFILE = 30-functionvoid.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/FunctionVoid/functionvoid.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * functionvoid.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a void function call.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 | #include
13 |
14 | /**
15 | * Namespace to use
16 | */
17 | using namespace std;
18 |
19 | /**
20 | * my_function_void()
21 | */
22 | void my_function_void()
23 | {
24 | cout << "In my_function_void()" << endl;
25 | }
26 |
27 |
28 | // Symbols are exported according to the "C" language
29 | extern "C"
30 | {
31 | // export the "get_module" function that will be called by the Zend engine
32 | PHPCPP_EXPORT void *get_module()
33 | {
34 | // create extension
35 | static Php::Extension extension("my_function_void","1.0");
36 |
37 | // add function to extension
38 | extension.add("my_void_function");
39 |
40 | // return the extension module
41 | return extension.module();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Examples/FunctionVoid/functionvoid.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a void function call.
7 | */
8 |
9 | /*
10 | * Run the function with the given name. As you can see, the given name
11 | * can be different from the actual function name.
12 | */
13 | my_void_function();
14 |
--------------------------------------------------------------------------------
/Examples/FunctionWithParameters/30-functionwithparameters.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=functionwithparameters.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/FunctionWithParameters/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = functionwithparameters.so
12 |
13 | PHPINIFILE = 30-functionwithparameters.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/FunctionWithParameters/functionwithparameters.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a function call with parameters, defined and undefined.
7 | */
8 |
9 | /*
10 | * Test class.
11 | */
12 | class MyPhpClass {
13 |
14 | public $aMemberVar = "aMemberVar";
15 |
16 | public function __toString()
17 | {
18 | return "myPhpClass.__toString()";
19 | }
20 |
21 | public function getMemberVar()
22 | {
23 | return $aMemberVar;
24 | }
25 | }
26 |
27 | /*
28 | * Run a function with parameters.
29 | */
30 |
31 |
32 | /*
33 | * A function which takes parameters, which are all undefined;
34 | * my_with_undefined_parameters_function('1st','2nd','3rd','4th')
35 | */
36 | echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n";
37 |
38 | /*
39 | * A function which takes parameters, which are all defined;
40 | * my_with_defined_parameters_function(21,42)
41 | */
42 |
43 | echo my_with_defined_parameters_function(21,42) . "\n\n\n";
44 |
45 | /*
46 | * A function which takes a reference of a parameter
47 | * my_with_defined_parameters_reference_function(referenceVar)
48 | */
49 |
50 | $referenceVar = "I am unchanged.";
51 | echo "The value of referenceVar: " . $referenceVar. "\n";
52 |
53 | echo my_with_defined_parameters_reference_function($referenceVar) . "\n";
54 |
55 | echo "New value of referenceVar: " . $referenceVar ."\n\n\n";
56 |
57 | /*
58 | * A function which takes an array as a parameter
59 | * my_with_defined_array_parameters_function($myArray)
60 | */
61 | $myArray = array("a", "b", "c", "d");
62 | echo my_with_defined_array_parameters_function($myArray) . "\n\n\n";
63 |
64 | /*
65 | * A function which takes an object as a parameter
66 | * my_with_defined_object_parameter_function(myPhpClass)
67 | */
68 |
69 | $myPhpClass = new MyPhpClass;
70 | echo my_with_defined_object_parameters_function($myPhpClass);
71 |
72 | /*
73 | * Accessing a non-existant parameters index will result in a segmentation fault.
74 | * The segmentation fault will also occur when passing the wrong type of parameter.
75 | */
76 |
--------------------------------------------------------------------------------
/Examples/Globals/30-globals.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=globals.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/Globals/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -shared -O2
11 | RESULT = globals.so
12 |
13 | PHPINIFILE = 30-globals.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/Globals/globals.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * functionvoid.cpp
3 | * @author Emiel Bruijntjes
4 | *
5 | * An example file to show how global variables can be accessed
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 | #include
13 |
14 | /**
15 | * Namespace to use
16 | */
17 | using namespace std;
18 |
19 | /**
20 | * process_globals()
21 | *
22 | * This function reads and modifies global variables
23 | */
24 | Php::Value process_globals()
25 | {
26 | // all global variables can be accessed via the Php::GLOBALS variable,
27 | // which is more or less the same as the PHP $_GLOBALS variable
28 |
29 | // set a global variable
30 | Php::GLOBALS["a"] = 1;
31 |
32 | // increment a global variable
33 | Php::GLOBALS["b"] += 1;
34 |
35 | // set a global variable to be an array
36 | Php::GLOBALS["c"] = Php::Array();
37 |
38 | // add a member to an array
39 | Php::GLOBALS["c"]["member"] = 123;
40 |
41 | // and increment it
42 | Php::GLOBALS["c"]["member"] += 77;
43 |
44 | // change value e
45 | Php::GLOBALS["e"] = Php::GLOBALS["e"][0]("hello");
46 |
47 | // if a global variable holds a function, we can call it
48 | return Php::GLOBALS["d"](1,2,3);
49 | }
50 |
51 | // Symbols are exported according to the "C" language
52 | extern "C"
53 | {
54 | // export the "get_module" function that will be called by the Zend engine
55 | PHPCPP_EXPORT void *get_module()
56 | {
57 | // create extension
58 | static Php::Extension extension("globals","1.0");
59 |
60 | // add function to extension
61 | extension.add("process_globals");
62 |
63 | // return the extension module
64 | return extension.module();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/Examples/Globals/globals.php:
--------------------------------------------------------------------------------
1 |
5 | *
6 | * An example file to show the working of a void function call.
7 | */
8 |
9 | // we first need to set a number of globals
10 | $b = 10;
11 | $d = function($a,$b,$c) {
12 | return $a+$b+$c;
13 | };
14 |
15 | // fun global var
16 | $e = array(
17 | function($a) {
18 | return $a;
19 | }
20 | );
21 |
22 |
23 | // call the C++ function that will do something
24 | $d = process_globals();
25 |
26 | // the global variable $a should not have the value 1
27 | echo("a = $a\n");
28 |
29 | // the variable $b should not be 11
30 | echo("b = $b\n");
31 |
32 | // $c should be an array with value 200
33 | echo("c['member'] = ".$c['member']."\n");
34 |
35 | // $d is overwritten and now is 6
36 | echo("d = $d\n");
37 |
38 | // e should be replaced by a string
39 | echo("e = $e\n");
--------------------------------------------------------------------------------
/Examples/IncludeOnce/30-includeonce.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=includeonce.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/IncludeOnce/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
4 | PHP_CONFIG = $(shell which php-config)
5 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
6 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
7 |
8 | LD = g++
9 | LD_FLAGS = -Wall -shared -O2
10 | RESULT = includeonce.so
11 |
12 | PHPINIFILE = 30-includeonce.ini
13 |
14 | SOURCES = $(wildcard *.cpp)
15 | OBJECTS = $(SOURCES:%.cpp=%.o)
16 |
17 | all: ${OBJECTS} ${RESULT}
18 |
19 | ${RESULT}: ${OBJECTS}
20 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
21 |
22 | clean:
23 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
24 |
25 | ${OBJECTS}:
26 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
27 |
28 | install:
29 | cp -f ${RESULT} ${LIBRARY_DIR}/
30 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
31 |
32 | uninstall:
33 | rm ${LIBRARY_DIR}/${RESULT}
34 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
35 |
--------------------------------------------------------------------------------
/Examples/IncludeOnce/class.php:
--------------------------------------------------------------------------------
1 |
9 | * @copyright 2024 Copernica BV
10 | */
11 |
12 | /**
13 | * Include ourselves (should do nothing)
14 | */
15 | include_once(__FILE__);
16 | my_include_once(__FILE__);
17 |
18 | /**
19 | * Class definition
20 | */
21 | class SillyClass {}
22 |
--------------------------------------------------------------------------------
/Examples/IncludeOnce/includeonce.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * callphpfunction.cpp
3 | * @author Jasper van Eck
4 | *
5 | * An example file to show the working of a php function call in C++.
6 | */
7 |
8 | /**
9 | * Libraries used.
10 | */
11 | #include
12 | #include
13 |
14 | /**
15 | * Namespace to use
16 | */
17 | using namespace std;
18 |
19 | /**
20 | * Function to test the Php::include_once() call
21 | * @param ¶ms
22 | */
23 | void my_include_once(Php::Parameters ¶ms)
24 | {
25 | // the string
26 | Php::Value path = params[0];
27 |
28 | // do the call
29 | Php::include_once(path.stringValue());
30 | }
31 |
32 |
33 | // Symbols are exported according to the "C" language
34 | extern "C"
35 | {
36 | // export the "get_module" function that will be called by the Zend engine
37 | PHPCPP_EXPORT void *get_module()
38 | {
39 | // create extension
40 | static Php::Extension extension("include_once","1.0");
41 |
42 | // add function to extension
43 | extension.add("my_include_once", {
44 | Php::ByVal("path", Php::Type::String),
45 | });
46 |
47 | // return the extension module
48 | return extension.module();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Examples/IncludeOnce/includeonce.php:
--------------------------------------------------------------------------------
1 |
7 | * @copyright 2015 Copernica BV
8 | */
9 |
10 | /**
11 | * Include guard
12 | */
13 | #pragma once
14 |
15 | /**
16 | * Class definition
17 | */
18 | class Child : public Php::Base
19 | {
20 | public:
21 | /**
22 | * Constructor and destructor
23 | */
24 | Child() {}
25 | virtual ~Child() {}
26 |
27 | /**
28 | * Cast to a string
29 | * @return const char *
30 | */
31 | const char *__toString() const
32 | {
33 | return "this is the child";
34 | }
35 | };
36 |
37 |
--------------------------------------------------------------------------------
/Examples/ReturnObject/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "master.h"
3 | #include "child.h"
4 |
5 | /**
6 | * tell the compiler that the get_module is a pure C function
7 | */
8 | extern "C" {
9 |
10 | /**
11 | * Function that is called by PHP right after the PHP process
12 | * has started, and that returns an address of an internal PHP
13 | * strucure with all the details and features of your extension
14 | *
15 | * @return void* a pointer to an address that is understood by PHP
16 | */
17 | PHPCPP_EXPORT void *get_module()
18 | {
19 | // static(!) Php::Extension object that should stay in memory
20 | // for the entire duration of the process (that's why it's static)
21 | static Php::Extension extension("returnobjecy", "1.0");
22 |
23 | // we have to class - master and child
24 | Php::Class master("master");
25 | Php::Class child("child");
26 |
27 | // the master class has one method - to return a child
28 | master.method<&Master::child>("child");
29 |
30 | // add all classes to the extension
31 | extension.add(master);
32 | extension.add(child);
33 |
34 | // return the extension
35 | return extension;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Examples/ReturnObject/master.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Master.h
3 | *
4 | * Class that is exported to PHP space
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2015 Copernica BV
8 | */
9 |
10 | /**
11 | * Include guard
12 | */
13 | #pragma once
14 |
15 | /**
16 | * Dependencies
17 | */
18 | #include "child.h"
19 |
20 | /**
21 | * Class definition
22 | */
23 | class Master : public Php::Base
24 | {
25 | private:
26 | /**
27 | * One child
28 | * @var Php::Value
29 | */
30 | Php::Value _value;
31 |
32 | public:
33 | /**
34 | * Constructor
35 | */
36 | Master()
37 | {
38 | // create a child instance
39 | _value = Php::Object("Child", new Child());
40 | }
41 |
42 | /**
43 | * Destructor
44 | */
45 | virtual ~Master() {}
46 |
47 | /**
48 | * Retrieve the child
49 | * @return Php::Value
50 | */
51 | Php::Value child() const
52 | {
53 | return _value;
54 | }
55 |
56 | /**
57 | * Cast to a string
58 | * @return const char *
59 | */
60 | const char *__toString() const
61 | {
62 | return "this is the master";
63 | }
64 | };
65 |
66 |
--------------------------------------------------------------------------------
/Examples/ReturnObject/returnobject.ini:
--------------------------------------------------------------------------------
1 | extension=returnobject.so
2 |
--------------------------------------------------------------------------------
/Examples/ReturnObject/test.php:
--------------------------------------------------------------------------------
1 |
6 | * @copyright 2015 Copernica BV
7 | */
8 |
9 | /**
10 | * Construct master object
11 | * @var Master
12 | */
13 | $master = new Master();
14 |
15 | /**
16 | * Construct derived child object
17 | * @var Child
18 | */
19 | $child1 = new Child();
20 |
21 | /**
22 | * Fetch the child object that is stored as member var in the master
23 | * @var Child
24 | */
25 | $child2 = $master->child();
26 |
27 | /**
28 | * Show output, expected is:
29 | * this is the master
30 | * this is the child
31 | * this is the child
32 | */
33 | echo(strval($master)."\n");
34 | echo(strval($child1)."\n");
35 | echo(strval($child2)."\n");
36 |
37 |
--------------------------------------------------------------------------------
/Examples/simple/30-phpcpp.ini:
--------------------------------------------------------------------------------
1 | ; configuration for phpcpp module
2 | ; priority=30
3 | extension=simple.so
4 |
5 |
--------------------------------------------------------------------------------
/Examples/simple/Makefile:
--------------------------------------------------------------------------------
1 | CPP = g++
2 | RM = rm -f
3 | CPP_FLAGS = -Wall -c -I. -I/home/work/include/ -O2 -std=c++11
4 |
5 | PHP_CONFIG = $(shell which php-config)
6 | LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir)
7 | PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir)
8 |
9 | LD = g++
10 | LD_FLAGS = -Wall -Wl,-rpath,/home/work/lib -shared -O2 -L/home/work/lib
11 | RESULT = simple.so
12 |
13 | PHPINIFILE = 30-phpcpp.ini
14 |
15 | SOURCES = $(wildcard *.cpp)
16 | OBJECTS = $(SOURCES:%.cpp=%.o)
17 |
18 | all: ${OBJECTS} ${RESULT}
19 |
20 | ${RESULT}: ${OBJECTS}
21 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
22 |
23 | clean:
24 | ${RM} *.obj *~* ${OBJECTS} ${RESULT}
25 |
26 | ${OBJECTS}:
27 | ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
28 |
29 | install:
30 | cp -f ${RESULT} ${LIBRARY_DIR}/
31 | cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/
32 |
33 | uninstall:
34 | rm ${LIBRARY_DIR}/${RESULT}
35 | rm ${PHP_CONFIG_DIR}/${PHPINIFILE}
36 |
--------------------------------------------------------------------------------
/Examples/simple/simple.php:
--------------------------------------------------------------------------------
1 | a."\n");
62 | // echo($this->b."\n");
63 | // echo("hoi\n");
64 | //
65 | // parent::myMethod($val);
66 | // }
67 | //
68 | //}
69 | //
70 | //$x = new my_extended_class();
71 | //$x->myMethod(123);
72 |
73 | $x = new my_class();
74 | $x->myMethod();
75 |
76 |
77 | //echo(my_plus(1,2,3,4)."\n");
78 |
79 | $array = array();
80 | for ($i=0; $i<10000; $i++) $array[] = rand();
81 |
82 | //$array = array(1,2,3);
83 |
84 | //print_r($array);
85 | //bubblesort($array);
86 |
87 | //print_r($array);
88 |
89 |
90 | //echo("my_class::a = ".$x->a."\n");
91 | //echo("my_class::b = ".$x->b."\n");
92 | //
93 | //unset($x);
94 | //
95 | //echo("done\n");
96 |
97 | //$x->my_method();
98 |
99 |
100 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | PHP-CPP
2 | =======
3 |
4 | [](https://travis-ci.org/CopernicaMarketingSoftware/PHP-CPP)
5 |
6 | The PHP-CPP library is a C++ library for developing PHP extensions. It offers a collection
7 | of well documented and easy-to-use classes that can be used and extended to build native
8 | extensions for PHP. The full documentation can be found on http://www.php-cpp.com.
9 |
10 | ABOUT
11 | =====
12 |
13 | PHP-CPP is created and maintained by Copernica (www.copernica.com). We write
14 | our code mostly in PHP and C++ and needed an effective way
15 | to combine these two languages. That's where PHP-CPP comes in.
16 | Do you appreciate our work and are you looking for other high quality solutions?
17 |
18 | Then check out our other solutions:
19 |
20 | * PHP-JS (www.php-js.com)
21 | * Copernica Marketing Suite (www.copernica.com)
22 | * MailerQ MTA (www.mailerq.com)
23 | * Responsive Email web service (www.responsiveemail.com)
24 |
25 | The power of PHP-CPP
26 | ====================
27 |
28 | Unlike regular PHP extensions - which are really hard to implement and require a deep
29 | knowledge of the Zend engine and pointer manipulation - extensions built with PHP-CPP
30 | are not difficult to develop at all. In fact, the only thing you need to do is write a function in
31 | C++, and the PHP-CPP library uses all the power offered by C++11 to convert the parameters and return
32 | values from your functions to/and from PHP:
33 |
34 | ```c
35 | Php::Value hello_world()
36 | {
37 | return "hello world!";
38 | }
39 | ```
40 |
41 | The function above is a native C++ function. With PHP-CPP you can export this function
42 | to PHP with only one single C++ method call:
43 |
44 | ```c
45 | extension.add("hello_world", hello_world);
46 | ```
47 |
48 | Working with parameters and return values is just as easy:
49 |
50 | ```c
51 | Php::Value my_plus(Php::Parameters ¶ms)
52 | {
53 | return params[0] + params[1];
54 | }
55 | ```
56 |
57 | The method call to export the above C++ function:
58 |
59 | ```c
60 | extension.add("my_plus", {
61 | Php::ByVal("a", Php::numericType),
62 | Php::ByVal("b", Php::numericType)
63 | });
64 | ```
65 |
66 | The PHP-CPP library ensures that the variables
67 | from PHP (which internally are complicated C structures), are automatically converted into
68 | integers, passed to your function, and that the return value of your "my_plus" function is
69 | also converted back into a PHP variable.
70 |
71 | Type conversion between native C/C++ types and PHP variables is handled by PHP-CPP, using
72 | features from the C++11 language. It does not matter if your functions accept strings,
73 | integers, booleans or other native parameters: PHP-CPP takes care of the conversion.
74 | The return value of your function is also transformed by PHP-CPP into PHP.
75 |
76 | More complicated structures can be handled by PHP-CPP as well. If you would like to return
77 | a nested associative array from your function, you can do so too:
78 |
79 | ```c
80 | Php::Value get_complex_array()
81 | {
82 | Php::Value r;
83 | r["a"] = 123;
84 | r["b"] = 456;
85 | r["c"][0] = "nested value";
86 | r["c"][1] = "example";
87 | return r;
88 | }
89 | ```
90 |
91 | The C++ function above is equivalent to the following function in PHP:
92 |
93 | ```php
94 | function get_complex_array()
95 | {
96 | return array(
97 | "a" => 123,
98 | "b" => 456,
99 | "c" => array("nested_value","example")
100 | );
101 | }
102 | ```
103 |
104 | More information and more examples are available on the official website:
105 | http://www.php-cpp.com.
106 |
--------------------------------------------------------------------------------
/common/includes.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Includes.h
3 | *
4 | * All includes for compiling the common module files of PHP-CPP library
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2014 Copernica BV
8 | */
9 |
10 | /**
11 | * Standard C and C++ libraries
12 | */
13 | #include
14 |
15 | /**
16 | * Public include files
17 | */
18 | #include "../include/visibility.h"
19 | #include "../include/modifiers.h"
20 |
21 | /**
22 | * Generic implementation header files
23 | */
24 | #include "streambuf.h"
25 |
--------------------------------------------------------------------------------
/common/modifiers.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Modifiers.cpp
3 | *
4 | * In this file an enumeration type is with the possible
5 | * member modifiers
6 | *
7 | * @author Martijn Otto
8 | * @author Emiel Bruijntjes
9 | *
10 | * @copyright 2014 Copernica BV
11 | */
12 | #include "includes.h"
13 | #include
14 |
15 | /**
16 | * Set up namespace
17 | */
18 | namespace Php {
19 |
20 | /**
21 | * The modifiers are constants
22 | */
23 | #if PHP_VERSION_ID < 70400
24 | const int Static = 0x01;
25 | const int Abstract = 0x02;
26 | const int Final = 0x04;
27 | const int Public = 0x100;
28 | const int Protected = 0x200;
29 | const int Private = 0x400;
30 | const int Const = 0;
31 | #else
32 | const int Static = 0x10;
33 | const int Abstract = 0x40;
34 | const int Final = 0x20;
35 | const int Public = 0x01;
36 | const int Protected = 0x02;
37 | const int Private = 0x04;
38 | const int Const = 0;
39 | #endif
40 |
41 | /**
42 | * Modifiers that are supported for methods and properties
43 | */
44 | const int MethodModifiers = Final | Public | Protected | Private | Static;
45 | const int PropertyModifiers = Final | Public | Protected | Private | Const | Static;
46 |
47 | /**
48 | * End namespace
49 | */
50 | }
51 |
--------------------------------------------------------------------------------
/common/streambuf.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * StreamBuf.cpp
3 | *
4 | * Implementation file for the StreamBuf class
5 | *
6 | * @see http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2014 Copernica BV
10 | */
11 | #include "includes.h"
12 |
13 | /**
14 | * Set up namespace
15 | */
16 | namespace Php {
17 |
18 | /**
19 | * Constructor
20 | * @param error
21 | */
22 | StreamBuf::StreamBuf(int error) : _error(error)
23 | {
24 | // we reserve one byte, so that when overflow is called, we still have one
25 | // byte extra in the buffer to put the overflowed byte int
26 | setp(_buffer, _buffer+1024-1);
27 | }
28 |
29 |
30 | /**
31 | * Method that is called when the internal buffer overflows
32 | * @param c
33 | * @return int
34 | */
35 | int StreamBuf::overflow(int c)
36 | {
37 | // for error buffers, overflow is simply discarded
38 | if (_error) return c;
39 |
40 | // end-of-file has not output, we call EOF directly, and by using the
41 | // comma operator we ensure that EOF is returned
42 | if (c == EOF) return sync(), EOF;
43 |
44 | // because we lied the underlying buffer about the size of the buffer
45 | // by one byte, there is no real overflow, and we can still add the byte
46 | // to the end of the buffer
47 | *pptr() = c;
48 |
49 | // increment buffer size
50 | pbump(1);
51 |
52 | // and now we're going to syn the buffer
53 | return sync() == -1 ? EOF : c;
54 | }
55 |
56 | /**
57 | * End namespace
58 | */
59 | }
60 |
--------------------------------------------------------------------------------
/common/streambuf.h:
--------------------------------------------------------------------------------
1 | /**
2 | * StreamBuf.h
3 | *
4 | * PHP output stream buffer which is used by the Php::out object to
5 | * have an output stream just like the regular std::ostream buffers,
6 | * but that sends all output to PHP output
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2014 Copernica BV
10 | */
11 |
12 | /**
13 | * Set up namespace
14 | */
15 | namespace Php {
16 |
17 | /**
18 | * Issue reported by user on a Windows / Mingw32 platform: EOF was
19 | * not defined. Let's defined it ourselves
20 | */
21 | #ifndef EOF
22 | # define EOF (-1)
23 | #endif
24 |
25 | /**
26 | * Class definition
27 | */
28 | class StreamBuf : public std::streambuf
29 | {
30 | public:
31 | /**
32 | * Constructor
33 | * @param error the error type, or 0 for regular output
34 | */
35 | StreamBuf(int error);
36 |
37 | /**
38 | * No copying or moving
39 | * @param that
40 | */
41 | StreamBuf(const StreamBuf &that) = delete;
42 | StreamBuf(StreamBuf &&that) = delete;
43 |
44 | /**
45 | * Destructor
46 | */
47 | virtual ~StreamBuf() {}
48 |
49 | /**
50 | * No copying or moving
51 | * @param that
52 | */
53 | StreamBuf &operator=(const StreamBuf &that) = delete;
54 | StreamBuf &operator=(StreamBuf &&that) = delete;
55 |
56 | protected:
57 | /**
58 | * Method that is called when the internal buffer overflows
59 | * @param c
60 | * @return int
61 | */
62 | virtual int overflow(int c = EOF) override;
63 |
64 | /**
65 | * Called when the internal buffer should be synchronized
66 | * @return int
67 | */
68 | virtual int sync() override;
69 |
70 | private:
71 | /**
72 | * The error type, or 0 for regular output
73 | * @var int
74 | */
75 | int _error;
76 |
77 | /**
78 | * The internal buffer
79 | * @var char[]
80 | */
81 | char _buffer[1024];
82 | };
83 |
84 | /**
85 | * End namespace
86 | */
87 | }
88 |
89 |
--------------------------------------------------------------------------------
/documentation/ini.html:
--------------------------------------------------------------------------------
1 | Reading php.ini variables
2 |
3 | Reading settings from the php.ini file(s) is just as simple as it
4 | is to obtain these settings from a regular PHP script. Inside a PHP script
5 | you can use the built-in ini_get() function to read settings from the php.ini
6 | file, and in your C++ extension you use the Php::ini_get() function.
7 |
8 |
9 |
#include <phpcpp.h>
10 |
11 | /**
12 | * Simple function that is used to demonstrate how settings from the
13 | * php.ini file can be read
14 | */
15 | void myFunction()
16 | {
17 | // read in the "output_buffering" variable from the php.ini file
18 | int output_buffering = Php::ini_get("output_buffering");
19 |
20 | // read in the "variables_order" variable
21 | std::string variables_order = Php::ini_get("variables_order");
22 | }
23 |
24 | /**
25 | * Switch to C contect so that the get_module() function can be
26 | * called by the Zend engine
27 | */
28 | extern "C" {
29 | /**
30 | * The get_module() startup function
31 | * @return void*
32 | */
33 | PHPCPP_EXPORT void *get_module() {
34 |
35 | // create extension object
36 | static Php::Extension extension("my_extension", "1.0");
37 |
38 | // export one function
39 | extension.add("myFunction", myFunction);
40 |
41 | // return a pointer to the extension object
42 | return extension;
43 | }
44 | }
45 |
46 |
47 | The Php::ini_get() function returns an object that can be assigned to
48 | strings, integers and floating point numbers. In the above example we have
49 | used this to assign the settings directly to an integer and a std::string.
50 |
51 |
52 | You can only retrieve predefined variables from the php.ini. It is thus not
53 | possible to call Php::ini_get() with random strings. If you want to use
54 | your own variables, you must first register them in the get_module()
55 | function before you can call Php::ini_get() to retrieve the current values.
56 |
57 |
58 |
#include <phpcpp.h>
59 |
60 | /**
61 | * Simple function that is used to demonstrate how settings from the
62 | * php.ini file can be read
63 | */
64 | void myFunction()
65 | {
66 | // read in a variable defined for this extension
67 | int var1 = Php::ini_get("my_extension.var1");
68 |
69 | // read in a string variable
70 | std::string var2 = Php::ini_get("my_extension.var2");
71 | }
72 |
73 | /**
74 | * Switch to C contect so that the get_module() function can be
75 | * called by the Zend engine
76 | */
77 | extern "C" {
78 | /**
79 | * The get_module() startup function
80 | * @return void*
81 | */
82 | PHPCPP_EXPORT void *get_module() {
83 |
84 | // create extension object
85 | static Php::Extension extension("my_extension", "1.0");
86 |
87 | // export one function
88 | extension.add("myFunction", myFunction);
89 |
90 | // tell the PHP engine that the php.ini variables my_extension.var1
91 | // and my_extension.var2 are usable
92 | extension.add(Php::Ini("my_extension.var1", "default-value"));
93 | extension.add(Php::Ini("my_extension.var2", 12345));
94 |
95 | // return a pointer to the extension object
96 | return extension;
97 | }
98 | }
99 |
100 |
--------------------------------------------------------------------------------
/documentation/namespaces.html:
--------------------------------------------------------------------------------
1 | Namespaces
2 |
3 | Although namespaces have a very rich implementation in PHP scripts,
4 | with special keyswords like 'use' and 'namespace' and special constants
5 | like '__NAMESPACE__', they are internally very simple.
6 |
7 |
8 | A namespace is nothing else than a class or function prefix. If you want your classes
9 | or functions to appear in a specific namespace, you simply have to add
10 | a prefix to the class or function name. The following code creates a
11 | function "myFunction" in the "myNamespace" namespace.
12 |
13 |
14 |
#include <phpcpp.h>
15 |
16 | // function that we're going to export
17 | void myFunction()
18 | {
19 | }
20 |
21 | extern "C" {
22 | PHPCPP_EXPORT void *get_module() {
23 |
24 | // create extension object
25 | static Php::Extension extension("my_extension", "1.0");
26 |
27 | // add the myFunction function to the extension,
28 | // and put it in namespace "myNamespace"
29 | extension.add("myNamespace\\myFunction", myFunction);
30 |
31 | // return the extension
32 | return extension;
33 | }
34 | }
35 |
36 |
37 | If you like, you can use the Php::Namespace utility class for this. This is a
38 | class with exactly the same signature as the Php::Extension class, and
39 | that you can use to register classes and functions too.
40 |
41 |
42 |
#include <phpcpp.h>
43 |
44 | // function that we're going to export
45 | void myFunction()
46 | {
47 | }
48 |
49 | extern "C" {
50 | PHPCPP_EXPORT void *get_module() {
51 |
52 | // create extension object
53 | static Php::Extension extension("my_extension", "1.0");
54 |
55 | // create a namespace
56 | Php::Namespace myNamespace("myNamespace");
57 |
58 | // add the myFunction function to the namespace
59 | myNamespace.add("myFunction", myFunction);
60 |
61 | // @todo add more functions and classes to the namespace
62 |
63 | // create a nested namespace
64 | Php::Namespace nestedNamespace("nestedNamespace");
65 |
66 | // @todo add functions and classes to the nested namespace
67 |
68 | // add the nested namespace to the first namespace
69 | myNamespace.add(std::move(nestedNamespace));
70 |
71 | // add the namespace to the extension
72 | extension.add(std::move(myNamespace));
73 |
74 | // return the extension
75 | return extension;
76 | }
77 | }
78 |
79 |
80 | The Php::Namespace class is just a container that automatically adds a
81 | prefix to all classes and functions that you add to it. Nesting namespaces
82 | is possible too, as you saw in the example.
83 |
84 |
85 | In the example we used the std::move() function to move the nested namespace
86 | into the parent namespace, and to move the first namespace into the extension.
87 | Moving is more efficient that adding, although a regular extension.add(myNamespace)
88 | would have been valid too.
89 |
90 |
--------------------------------------------------------------------------------
/include/array.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Array.h
3 | *
4 | * The Array is a wrapper around the value class that ensures that a
5 | * certain property always is an array
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2013 - 2019 Copernica BV
9 | */
10 |
11 | /**
12 | * Set up namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * Class definition
18 | */
19 | class PHPCPP_EXPORT Array : public Value
20 | {
21 | public:
22 | /**
23 | * Constructor
24 | */
25 | Array() : Value(Type::Array) {}
26 |
27 | /**
28 | * Copy constructor from a value object
29 | * @param value
30 | */
31 | Array(const Value &value) : Value(value)
32 | {
33 | // type must be valid
34 | if (value.type() != Type::Array) throw Error("Assigning a non-array to an array variable");
35 | }
36 |
37 | /**
38 | * Move constructor from a value object
39 | * @param value
40 | */
41 | Array(Value &&value) : Value(std::move(value))
42 | {
43 | // type must be valid
44 | if (value.type() != Type::Array) throw Error("Moving a non-array to an array variable");
45 | }
46 |
47 | /**
48 | * Constructors from a vector (this will create an array)
49 | * @param value
50 | */
51 | template
52 | Array(const std::vector &input) : Value(input) {}
53 |
54 | /**
55 | * Constructor from a map (this will create an associative array)
56 | * @param value
57 | */
58 | template
59 | Array(const std::map &value) : Value(value) {}
60 |
61 | // old visual c++ environments have no support for initializer lists
62 | # if !defined(_MSC_VER) || _MSC_VER >= 1800
63 |
64 | /**
65 | * Constructor from an initializer list
66 | * @param value
67 | */
68 | Array(const std::initializer_list &value) : Value(value) {}
69 |
70 | // end of visual c++ check
71 | # endif
72 |
73 | /**
74 | * Destructor
75 | */
76 | virtual ~Array() {}
77 |
78 | /**
79 | * Change the internal type of the variable
80 | * @param Type
81 | */
82 | virtual Value &setType(Type type) & override
83 | {
84 | // throw exception if things are going wrong
85 | if (type != Type::Array) throw Error("Changing type of a fixed array variable");
86 |
87 | // call base
88 | return Value::setType(Type::Array);
89 | }
90 |
91 | /**
92 | * Assignment operator
93 | * @param value
94 | * @return Array
95 | */
96 | Array &operator=(const Value &value)
97 | {
98 | // skip self assignment
99 | if (this == &value) return *this;
100 |
101 | // type must be valid
102 | if (value.type() != Type::Array) throw Error("Assigning a non-array to a fixed array variable");
103 |
104 | // call base
105 | Value::operator=(value);
106 |
107 | // done
108 | return *this;
109 | }
110 |
111 | /**
112 | * Move assignment operator
113 | * @param value
114 | * @return Array
115 | */
116 | Array &operator=(Value &&value)
117 | {
118 | // skip self assignment
119 | if (this == &value) return *this;
120 |
121 | // type must be valid
122 | if (value.type() != Type::Array) throw Error("Moving a non-array to a fixed array variable");
123 |
124 | // call base
125 | Value::operator=(std::move(value));
126 |
127 | // done
128 | return *this;
129 | }
130 | };
131 |
132 | /**
133 | * End of namespace
134 | */
135 | }
136 |
--------------------------------------------------------------------------------
/include/arrayaccess.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ArrayAccess.h
3 | *
4 | * "Interface" that can be "implemented" by your class. If you do, you
5 | * create your class like this:
6 | *
7 | * class MyClass : public Php::Base, public Php::ArrayAccess { ... }
8 | *
9 | * @author Emiel Bruijntjes
10 | * @copyright 2014 Copernica BV
11 | */
12 |
13 | /**
14 | * Set up namespace
15 | */
16 | namespace Php {
17 |
18 | /**
19 | * Class definition
20 | */
21 | class PHPCPP_EXPORT ArrayAccess
22 | {
23 | public:
24 | /**
25 | * Check if a member is set
26 | * @param key
27 | * @return bool
28 | */
29 | virtual bool offsetExists(const Php::Value &key) = 0;
30 |
31 | /**
32 | * Set a member
33 | * @param key
34 | * @param value
35 | */
36 | virtual void offsetSet(const Php::Value &key, const Php::Value &value) = 0;
37 |
38 | /**
39 | * Retrieve a member
40 | * @param key
41 | * @return value
42 | */
43 | virtual Php::Value offsetGet(const Php::Value &key) = 0;
44 |
45 | /**
46 | * Remove a member
47 | * @param key
48 | */
49 | virtual void offsetUnset(const Php::Value &key) = 0;
50 |
51 | /**
52 | * Destructor
53 | */
54 | virtual ~ArrayAccess() = default;
55 | };
56 |
57 | /**
58 | * End namespace
59 | */
60 | }
61 |
--------------------------------------------------------------------------------
/include/byref.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ByRef.h
3 | *
4 | * Overridden Argument class to specify by-reference function arguments
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Class definition
17 | */
18 | class PHPCPP_EXPORT ByRef : public Argument
19 | {
20 | public:
21 | /**
22 | * Constructor
23 | * @param name Name of the argument
24 | * @param type Argument type
25 | * @param required Is this argument required?
26 | */
27 | ByRef(const char *name, Type type = Type::Null, bool required = true) : Argument(name, type, required, true) {}
28 |
29 | /**
30 | * Constructor
31 | * @param name Name of the argument
32 | * @param classname Name of the class
33 | * @param nullable Can it be null?
34 | * @param required Is this argument required?
35 | */
36 | ByRef(const char *name, const char *classname, bool nullable = false, bool required = true) : Argument(name, classname, nullable, required, true) {}
37 |
38 | /**
39 | * Copy constructor
40 | * @param argument
41 | */
42 | ByRef(const ByRef &argument) : Argument(argument) {}
43 |
44 | /**
45 | * Move constructor
46 | * @param argument
47 | */
48 | ByRef(ByRef &&argument) _NOEXCEPT : Argument(argument) {}
49 |
50 | /**
51 | * Destructor
52 | */
53 | virtual ~ByRef() {}
54 | };
55 |
56 | /**
57 | * End of namespace
58 | */
59 | }
60 |
--------------------------------------------------------------------------------
/include/byval.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ByVal.h
3 | *
4 | * Overridden Argument class to specify by-value function arguments
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Class definition
17 | */
18 | class PHPCPP_EXPORT ByVal : public Argument
19 | {
20 | public:
21 | /**
22 | * Constructor
23 | * @param name Name of the argument
24 | * @param type Argument type
25 | * @param required Is this argument required?
26 | */
27 | ByVal(const char *name, Type type = Type::Null, bool required = true) : Argument(name, type, required, false) {}
28 |
29 | /**
30 | * Constructor
31 | * @param name Name of the argument
32 | * @param classname Name of the class
33 | * @param nullable Can it be null?
34 | * @param required Is this argument required?
35 | */
36 | ByVal(const char *name, const char *classname, bool nullable = false, bool required = true) : Argument(name, classname, nullable, required, false) {}
37 |
38 | /**
39 | * Copy constructor
40 | * @param argument
41 | */
42 | ByVal(const ByVal &argument) : Argument(argument) {}
43 |
44 | /**
45 | * Move constructor
46 | * @param argument
47 | */
48 | ByVal(ByVal &&argument) _NOEXCEPT : Argument(argument) {}
49 |
50 | /**
51 | * Destructor
52 | */
53 | virtual ~ByVal() {}
54 | };
55 |
56 | /**
57 | * End of namespace
58 | */
59 | }
60 |
--------------------------------------------------------------------------------
/include/classtype.h:
--------------------------------------------------------------------------------
1 | /**
2 | * @file classtype.h
3 | *
4 | * Internal class types enumeration.
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2014 - 2022 Copernica BV
8 | */
9 |
10 | /**
11 | * Set up namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Enumeration declaration
17 | * The definition of the variables can be found in the Zend directory,
18 | * so that we can use the macro's from the PHP header files.
19 | */
20 | enum class ClassType;
21 |
22 | /**
23 | * End namespace
24 | */
25 | }
26 |
--------------------------------------------------------------------------------
/include/countable.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Countable.h
3 | *
4 | * "Interface" that can be "implemented" by your class. If you do, you
5 | * create your class like this:
6 | *
7 | * class MyClass : public Php::Base, public Php::Countable { ... }
8 | *
9 | * You will have to implement the count() method, which should return the
10 | * number of elements in the object
11 | *
12 | * @author Emiel Bruijntjes
13 | * @copyright 2014 Copernica BV
14 | */
15 |
16 | /**
17 | * Set up namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Class definition
23 | */
24 | class PHPCPP_EXPORT Countable
25 | {
26 | public:
27 | /**
28 | * Retrieve the number of items in the class
29 | * @return long
30 | */
31 | virtual long count() = 0;
32 |
33 | };
34 |
35 | /**
36 | * End namespace
37 | */
38 | }
39 |
--------------------------------------------------------------------------------
/include/deprecated.h:
--------------------------------------------------------------------------------
1 | /**
2 | * deprecated.h
3 | *
4 | * This file defines macros to mark a function as deprecated
5 | * in a way that works across different compilers.
6 | *
7 | * It is used for functions that still work, but should no
8 | * longer be used because they may be removed in an upcoming
9 | * version of PHP-CPP
10 | *
11 | * @copyright 2016 Copernica B.V.
12 | */
13 |
14 | #if __cplusplus >= 201402L
15 | #define PHPCPP_DEPRECATED [[deprecated]]
16 | #elif defined __GNUC__
17 | #define PHPCPP_DEPRECATED __attribute__((deprecated))
18 | #elif defined(_MSC_VER)
19 | #define PHPCPP_DEPRECATED __declspec(deprecated)
20 | #endif
21 |
--------------------------------------------------------------------------------
/include/error.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Error.h
3 | *
4 | * Class for fatal programming errors. You should normally not catch
5 | * these exceptions, but let them bubble up to crash your script, but
6 | * if you insist, you can.
7 | *
8 | * In your C++ extension you can both throw and catch instances of these
9 | * class. If you throw an exception, it will bubble up to PHP space,
10 | * where the PHP script can then catch it.
11 | *
12 | * Error thrown from PHP space can also be caught in C++ code.
13 | * If you put your call to a PHP user space function inside a try-catch
14 | * block, you can catch the exceptions as Php::Error objects.
15 | *
16 | * @author Emiel Bruijntjes
17 | * @copyright 2019 Copernica BV
18 | */
19 |
20 | /**
21 | * Begin of namespace
22 | */
23 | namespace Php {
24 |
25 | /**
26 | * Class definition
27 | */
28 | class PHPCPP_EXPORT Error : public Throwable
29 | {
30 | protected:
31 | /**
32 | * Internal constructor to wrap around an exception object
33 | * @param object
34 | */
35 | Error(struct _zend_object *object) : Throwable(object) {}
36 |
37 | /**
38 | * Rethrow the exception / make sure that it ends up in PHP space
39 | */
40 | virtual void rethrow() override;
41 |
42 | public:
43 | /**
44 | * Public constructor
45 | * @param message
46 | */
47 | Error(const std::string &message) : Throwable(message) {}
48 |
49 | /**
50 | * Destructor
51 | */
52 | virtual ~Error() = default;
53 | };
54 |
55 | /**
56 | * End of namespace
57 | */
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/include/exception.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Exception.h
3 | *
4 | * Class for runtime errors. In your C++ extension you can both throw
5 | * and catch instances of these class. If you throw an exception, it
6 | * will bubble up to PHP space, where the PHP script can then catch it.
7 | *
8 | * Exceptions called from PHP space can also be caught in C++ code.
9 | * If you put your call to a PHP user space function inside a try-catch
10 | * block, you can catch the exceptions as Php::Exception objects.
11 | *
12 | * @author Emiel Bruijntjes
13 | * @copyright 2019 Copernica BV
14 | */
15 |
16 | /**
17 | * Begin of namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Class definition
23 | */
24 | class PHPCPP_EXPORT Exception : public Throwable
25 | {
26 | protected:
27 | /**
28 | * Internal constructor to wrap around an exception object
29 | * @param object
30 | */
31 | Exception(struct _zend_object *object) : Throwable(object) {}
32 |
33 | /**
34 | * Rethrow the exception / make sure that it ends up in PHP space
35 | */
36 | virtual void rethrow() override;
37 |
38 | public:
39 | /**
40 | * Public constructor
41 | * @param message
42 | */
43 | Exception(const std::string &message) : Throwable(message) {}
44 |
45 | /**
46 | * Destructor
47 | */
48 | virtual ~Exception() = default;
49 | };
50 |
51 | /**
52 | * End of namespace
53 | */
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/include/file.h:
--------------------------------------------------------------------------------
1 | /**
2 | * File.h
3 | *
4 | * Extended script, a PHP source file name can be passed to a Php::File object
5 | * to have it evaluated.
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2014 Copernica BV
9 | */
10 |
11 | /**
12 | * Forward declarations
13 | */
14 | struct _zend_string;
15 |
16 | /**
17 | * Set up namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Class definition
23 | */
24 | class PHPCPP_EXPORT File
25 | {
26 | public:
27 | /**
28 | * Constructor
29 | *
30 | * The constructor receives a filename as parameter. It uses the normal
31 | * PHP include path resolve algorithms to find the location of the file.
32 | *
33 | * @param name the filename
34 | * @param size size of the filename
35 | */
36 | File(const char *name, size_t size);
37 |
38 | /**
39 | * Alternative constructor with just a filename
40 | *
41 | * @param name the filename
42 | */
43 | File(const char *name) : File(name, ::strlen(name)) {}
44 |
45 | /**
46 | * Alternative constructor with a string object
47 | * @param name the filename
48 | */
49 | File(const std::string &name) : File(name.c_str(), name.size()) {}
50 |
51 | /**
52 | * Alternative constructor with a Value object
53 | * @param name the filename
54 | */
55 | File(const Value &value) : File(value.stringValue()) {}
56 |
57 | /**
58 | * Destructor
59 | */
60 | virtual ~File();
61 |
62 | /**
63 | * Does the file exist?
64 | * @return boolean
65 | */
66 | bool exists();
67 |
68 | /**
69 | * Is this a valid file?
70 | * @return boolean
71 | */
72 | bool valid();
73 |
74 | /**
75 | * Execute the file once (do nothing if the file already was executed)
76 | * @return Php::Value
77 | */
78 | Value once();
79 |
80 | /**
81 | * Execute the file
82 | * @return Php::Value
83 | */
84 | Value execute();
85 |
86 | private:
87 | /**
88 | * The original path
89 | * @var struct _zend_string*
90 | */
91 | struct _zend_string *_original = nullptr;
92 |
93 | /**
94 | * The full resolved path name
95 | * @var struct _zend_string*
96 | */
97 | struct _zend_string *_path = nullptr;
98 |
99 | /**
100 | * The opcodes of this file
101 | * @var std::unique_ptr
102 | */
103 | std::unique_ptr _opcodes;
104 |
105 | /**
106 | * Compile the file
107 | * @return bool
108 | */
109 | bool compile();
110 |
111 | };
112 |
113 | /**
114 | * End of namespace
115 | */
116 | }
117 |
--------------------------------------------------------------------------------
/include/function.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Function.h
3 | *
4 | * Small extension to the Value class that allows a value to be
5 | * constructed with a std::function.
6 | *
7 | * If you want to assign a std::function to a value, the following
8 | * piece of code won't work:
9 | *
10 | * Php::Value value([]() { .... });
11 | *
12 | * Because the passed in function would match with many of the possible
13 | * Value constructors. For that reason we have created a small and
14 | * simple Function class that can be used instead:
15 | *
16 | * Php::Function valu([]() { .... });
17 | *
18 | * A Php::Function is an extended Php::Value object, so can be used
19 | * in place of Php::Values all the time. The only difference is that
20 | * it has a different constructor
21 | *
22 | * @author Emiel Bruijntjes
23 | * @copyright 2015 Copernica BV
24 | */
25 |
26 | /**
27 | * Set up namespace
28 | */
29 | namespace Php {
30 |
31 | /**
32 | * Class definition
33 | */
34 | class PHPCPP_EXPORT Function : public Value
35 | {
36 | public:
37 | /**
38 | * Constructor to wrap a function that takes parameters
39 | * @param function The C++ function to be wrapped
40 | */
41 | Function(const std::function &function);
42 |
43 | /**
44 | * Constructor to wrap a function that does not accept parameters
45 | *
46 | * Old C++ compilers do not see a difference between std::function
47 | * objects based on the function signature, so these old compilers
48 | * do not see this method.
49 | *
50 | * @param function The C++ function to be wrapped
51 | */
52 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7) || __clang__
53 | Function(const std::function &function) : Function([function](Parameters ¶ms) -> Value {
54 |
55 | // call original function, forget about the parameters
56 | return function();
57 |
58 | }) {}
59 | #endif
60 |
61 | /**
62 | * Destructor
63 | */
64 | virtual ~Function() {}
65 |
66 | private:
67 | /**
68 | * Retrieve the class entry of the _functor class
69 | * @return _zend_class_entry
70 | */
71 | static struct _zend_class_entry *entry();
72 |
73 |
74 | };
75 |
76 | /**
77 | * End namespace
78 | */
79 | }
80 |
--------------------------------------------------------------------------------
/include/globals.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Globals.h
3 | *
4 | * Wrapper object that gives access to all global variables. You
5 | * can use it more or less the same as the $_GLOBALS object in
6 | * PHP.
7 | *
8 | * The global PHP variables are acessible via the Php::globals["varname"]
9 | * variables.
10 | *
11 | * @author Emiel Bruijntjes
12 | * @copyright 2013 Copernica BV
13 | */
14 |
15 | /**
16 | * Set up namespace
17 | */
18 | namespace Php {
19 |
20 | /**
21 | * Forward definitions
22 | */
23 | class Global;
24 |
25 | /**
26 | * Class definition
27 | */
28 | class PHPCPP_EXPORT Globals
29 | {
30 | public:
31 | /**
32 | * Disable copy and move operations
33 | */
34 | Globals(const Globals &globals) = delete;
35 | Globals(Globals &&globals) = delete;
36 |
37 | /**
38 | * Destructor
39 | */
40 | virtual ~Globals() {}
41 |
42 | /**
43 | * Get access to a global variable
44 | * @param name
45 | * @return Global
46 | */
47 | Global operator[](const char *name);
48 |
49 | /**
50 | * Get access to a global variable
51 | * @param name
52 | * @return Global
53 | */
54 | Global operator[](const std::string &name);
55 |
56 | private:
57 | /**
58 | * Constructor
59 | */
60 | Globals() {}
61 |
62 | public:
63 | /**
64 | * Get the one and only instance
65 | * @return Globals
66 | */
67 | static Globals &instance();
68 | };
69 |
70 | /**
71 | * We always have one instance of the GLOBALS instance
72 | * @var Globals
73 | */
74 | extern PHPCPP_EXPORT Globals &GLOBALS;
75 |
76 | /**
77 | * End of namespace
78 | */
79 | }
80 |
--------------------------------------------------------------------------------
/include/hashparent.h:
--------------------------------------------------------------------------------
1 | /**
2 | * HashParent.h
3 | *
4 | * Interface that is implemented by all objects that can be accessed with
5 | * array-access variables ([]). When the value of a hash-member is changed,
6 | * it will call one of the methods from this class to set the new property
7 | *
8 | * This is an internal class that you normally not need when writing
9 | * extensions. It is used by the PHP-CPP library when you use constructs
10 | * like value["x"]["y"] = 10;
11 | *
12 | * @author Emiel Bruijntjes
13 | * @copyright 2014 Copernica BV
14 | */
15 |
16 | /**
17 | * Set up namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Forwards
23 | */
24 | class Value;
25 |
26 | /**
27 | * Class definition
28 | */
29 | class PHPCPP_EXPORT HashParent
30 | {
31 | protected:
32 | /**
33 | * Protected constructor - users should not instantiate HashParent
34 | * objects themselved. Use a Value object instead.
35 | */
36 | HashParent() {}
37 |
38 | public:
39 | /**
40 | * Destructor
41 | */
42 | virtual ~HashParent() {}
43 |
44 | /**
45 | * Check if a certain key exists in the array/object
46 | * @param key
47 | * @return bool
48 | */
49 | virtual bool contains(const std::string &key) const = 0;
50 |
51 | /**
52 | * Check if a certain index exists in the array/object
53 | * @param key
54 | * @return bool
55 | */
56 | virtual bool contains(int index) const = 0;
57 |
58 | /**
59 | * Check if a certain index exists in the array/object
60 | * @param key
61 | * @return bool
62 | */
63 | virtual bool contains(const Value &index) const = 0;
64 |
65 | /**
66 | * Retrieve the value at a string index
67 | * @param key
68 | * @return Value
69 | */
70 | virtual Value get(const std::string &key) const = 0;
71 |
72 | /**
73 | * Retrieve the value at a numeric index
74 | * @param index
75 | * @return Value
76 | */
77 | virtual Value get(int index) const = 0;
78 |
79 | /**
80 | * Retrieve the value at a value index
81 | * @param key
82 | * @return Value
83 | */
84 | virtual Value get(const Value &key) const = 0;
85 |
86 | /**
87 | * Overwrite the value at a certain string index
88 | * @param key
89 | * @param value
90 | */
91 | virtual void set(const std::string &key, const Value &value) = 0;
92 |
93 | /**
94 | * Overwrite the value at a certain numeric index
95 | * @param index
96 | * @param value
97 | */
98 | virtual void set(int index, const Value &value) = 0;
99 |
100 | /**
101 | * Overwrite the value at a certain variant index
102 | * @param key
103 | * @param value
104 | */
105 | virtual void set(const Value &key, const Value &value) = 0;
106 |
107 | /**
108 | * Unset a member by its index
109 | * @param index
110 | */
111 | virtual void unset(int index) = 0;
112 |
113 | /**
114 | * Unset a member by its key
115 | * @param key
116 | */
117 | virtual void unset(const std::string &key) = 0;
118 |
119 | /**
120 | * Unset a member by its key
121 | * @param key
122 | */
123 | virtual void unset(const Value &key) = 0;
124 |
125 | };
126 |
127 | /**
128 | * End namespace
129 | */
130 | }
131 |
--------------------------------------------------------------------------------
/include/inivalue.h:
--------------------------------------------------------------------------------
1 | /**
2 | * IniValue.h
3 | *
4 | * Class IniValue designed for extracting values from ini entries
5 | *
6 | * @copyright 2014 Copernica BV
7 | */
8 |
9 | /**
10 | * Set up namespace
11 | */
12 | namespace Php {
13 |
14 | /**
15 | * Class IniValue designed for extracting values from ini entries.
16 | */
17 | class PHPCPP_EXPORT IniValue
18 | {
19 | public:
20 | /**
21 | * Constructors
22 | *
23 | * @param name Name of the php.ini variable
24 | * @param isorig Is the original value
25 | */
26 | IniValue(const char *name, const bool isorig) : _name(name), _isorig(isorig) {}
27 |
28 | /**
29 | * Cast to a 16-bit number
30 | * @return int16_t
31 | */
32 | operator int16_t () const
33 | {
34 | return static_cast(numericValue());
35 | }
36 |
37 | /**
38 | * Cast to a 32-bit number
39 | * @return int32_t
40 | */
41 | operator int32_t () const
42 | {
43 | return static_cast(numericValue());
44 | }
45 |
46 | /**
47 | * Cast to a 64-bit number
48 | * @return uint64_t
49 | */
50 | operator int64_t () const
51 | {
52 | return numericValue();
53 | }
54 |
55 | /**
56 | * Cast to a boolean
57 | * @return boolean
58 | */
59 | operator bool () const
60 | {
61 | return boolValue();
62 | }
63 |
64 | /**
65 | * Cast to a string
66 | * @return string
67 | */
68 | operator std::string () const
69 | {
70 | return stringValue();
71 | }
72 |
73 | /**
74 | * Cast to byte array
75 | * @return const char *
76 | */
77 | operator const char * () const
78 | {
79 | return rawValue();
80 | }
81 |
82 | /**
83 | * Cast to a floating point
84 | * @return double
85 | */
86 | operator double() const;
87 |
88 | /**
89 | * Retrieve the value as number
90 | *
91 | * We force this to be a int64_t because we assume that most
92 | * servers run 64 bits nowadays, and because we use int32_t, int64_t
93 | * almost everywhere, instead of 'long' and on OSX neither of
94 | * these intxx_t types is defined as 'long'...
95 | *
96 | * @return int64_t
97 | */
98 | int64_t numericValue() const;
99 |
100 | /**
101 | * Boolean value
102 | * @return bool
103 | */
104 | bool boolValue() const
105 | {
106 | return static_cast(numericValue());
107 | }
108 |
109 | /**
110 | * String value
111 | * @return std::string
112 | */
113 | std::string stringValue() const
114 | {
115 | auto value = rawValue();
116 | return std::string(value ? value : "");
117 | }
118 |
119 | /**
120 | * Get access to the raw buffer for read operations.
121 | * @return const char *
122 | */
123 | const char *rawValue() const;
124 |
125 |
126 | private:
127 |
128 |
129 | /**
130 | * ini entry name
131 | * @var std::string
132 | */
133 | std::string _name;
134 |
135 | /**
136 | * Is the orig value?
137 | * @var bool
138 | */
139 | bool _isorig = false;
140 | };
141 |
142 | /**
143 | * Custom output stream operator
144 | * @param stream
145 | * @param ini_val
146 | * @return ostream
147 | */
148 | PHPCPP_EXPORT std::ostream &operator<<(std::ostream &stream, const IniValue &ini_val);
149 |
150 |
151 | /**
152 | * End of namespace
153 | */
154 | }
155 |
--------------------------------------------------------------------------------
/include/interface.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Interface.h
3 | *
4 | * @copyright 2014 - 2022 Copernica BV
5 | * @author Emiel Bruijntjes
6 | */
7 |
8 | /**
9 | * Set up namespace
10 | */
11 | namespace Php {
12 |
13 | /**
14 | * Class definition
15 | */
16 | class PHPCPP_EXPORT Interface : private ClassBase
17 | {
18 | public:
19 | /**
20 | * Constructor
21 | * @param name
22 | */
23 | Interface(const char *name);
24 |
25 | /**
26 | * Destructor
27 | */
28 | virtual ~Interface() = default;
29 |
30 | /**
31 | * Add a - of course abstract - method to the interface
32 | * @param name Name of the method
33 | * @param arguments Optional description of the arguments
34 | * @return Interface Same object to allow chaining
35 | */
36 | Interface &method(const char *name, const Arguments &arguments = {})
37 | {
38 | // call base
39 | ClassBase::method(name, Abstract | Public, arguments);
40 |
41 | // return self
42 | return *this;
43 | }
44 |
45 | /**
46 | * Add a - of course abstract - method to the interface
47 | * @param name Name of the method
48 | * @param flags Optional flags
49 | * @param arguments Optional description of the arguments
50 | * @return Interface Same object to allow chaining
51 | */
52 | Interface &method(const char *name, int flags, const Arguments &arguments = {})
53 | {
54 | // call base (an interface method is always public, so we add these flags,
55 | // and although it is always abstract, PHP does not allow this flag, so we
56 | // remove it in case the extension programmer had set it)
57 | ClassBase::method(name, (Public | flags) & ~Abstract, arguments);
58 |
59 | // return self
60 | return *this;
61 | }
62 |
63 | /**
64 | * Extends exisiting PHP interface
65 | *
66 | * Note that the interface that you supply must already exist! Therefore
67 | * you can only supply interfaces that you created in your own extension.
68 | *
69 | * @param interface Interface object
70 | * @return Interface Same object to allow chaining
71 | */
72 | Interface &extends(const Interface &interface) { ClassBase::implements(interface); return *this; }
73 |
74 | /**
75 | * The namespace needs to have access to the private ClassBase base
76 | * class, to actually register the interface.
77 | */
78 | friend class Namespace;
79 |
80 | /**
81 | * All Php::Class also need access to the base class to
82 | * register an interface.
83 | */
84 | template friend class Class;
85 | };
86 |
87 | /**
88 | * End namespace
89 | */
90 | }
91 |
--------------------------------------------------------------------------------
/include/iterator.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Iterator.h
3 | *
4 | * Base class for iterators. Extension writers that want to create traversable
5 | * classes, should override the Php::Traversable base class. This base class
6 | * forces you to implement a getIterator() method that returns an instance of
7 | * a Php::Iterator class.
8 | *
9 | * In this file you find the signature of the Php::Iterator class. It mostly has
10 | * pure virtual methods, which means that you should create a derived class
11 | * that implements all these methods.
12 | *
13 | * @author Emiel Bruijntjes
14 | * @copyright 2014 Copernica BV
15 | */
16 |
17 | /**
18 | * Set up namespace
19 | */
20 | namespace Php {
21 |
22 | /**
23 | * Class definition
24 | */
25 | class PHPCPP_EXPORT Iterator
26 | {
27 | public:
28 | /**
29 | * Constructor
30 | * @param base Class over which the iterator is iterating
31 | */
32 | Iterator(Base *base) : _object(base) {}
33 |
34 | /**
35 | * Destructor
36 | */
37 | virtual ~Iterator() {}
38 |
39 | /**
40 | * Is the iterator on a valid position
41 | * @return bool
42 | */
43 | virtual bool valid() = 0;
44 |
45 | /**
46 | * The value at the current position
47 | * @return Value
48 | */
49 | virtual Value current() = 0;
50 |
51 | /**
52 | * The key at the current position
53 | * @return Value
54 | */
55 | virtual Value key() = 0;
56 |
57 | /**
58 | * Move to the next position
59 | */
60 | virtual void next() = 0;
61 |
62 | /**
63 | * Rewind the iterator to the front position
64 | */
65 | virtual void rewind() = 0;
66 |
67 | protected:
68 | /**
69 | * During the lifetime of the iterator, the object over which
70 | * it iterates is keps as a private variable. This ensures that
71 | * this object is not destructed as long as the iterator exists
72 | * @var Value
73 | */
74 | Value _object;
75 |
76 | };
77 |
78 | /**
79 | * End namespace
80 | */
81 | }
82 |
--------------------------------------------------------------------------------
/include/message.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Message.h
3 | *
4 | * In this file an enumeration type is defined with all error-message flags.
5 | *
6 | * @author Toon Schoenmakers
7 | * @copyright 2015 - 2019 Copernica BV
8 | */
9 |
10 | /**
11 | * Set up namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Supported types of errors, this is mostly a copy from Zend/zend_errors.h
17 | */
18 | enum class Message : int {
19 | Error = (1 << 0L),
20 | Warning = (1 << 1L),
21 | Parse = (1 << 2L),
22 | Notice = (1 << 3L),
23 | CoreError = (1 << 4L),
24 | CoreWarning = (1 << 5L),
25 | CompileError = (1 << 6L),
26 | CompileWarning = (1 << 7L),
27 | UserError = (1 << 8L),
28 | UserWarning = (1 << 9L),
29 | UserNotice = (1 << 10L),
30 | Strict = (1 << 11L),
31 | RecoverableError = (1 << 12L),
32 | Deprecated = (1 << 13L),
33 | UserDeprecated = (1 << 14L),
34 |
35 | Core = (CoreError | CoreWarning),
36 | All = (Error | Warning | Parse | Notice | CoreError | CoreWarning | CompileError | CompileWarning | UserError | UserWarning | UserNotice | RecoverableError | Deprecated | UserDeprecated )
37 | };
38 |
39 | /**
40 | * End of namespace
41 | */
42 | }
43 |
--------------------------------------------------------------------------------
/include/modifiers.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Modifiers.h
3 | *
4 | * In this file an enumeration type is with the possible
5 | * member modifiers
6 | *
7 | * @author Martijn Otto
8 | * @copyright 2014 Copernica BV
9 | */
10 |
11 | /**
12 | * Set up namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * The modifiers are constants
18 | */
19 | extern PHPCPP_EXPORT const int Static;
20 | extern PHPCPP_EXPORT const int Abstract;
21 | extern PHPCPP_EXPORT const int Final;
22 | extern PHPCPP_EXPORT const int Public;
23 | extern PHPCPP_EXPORT const int Protected;
24 | extern PHPCPP_EXPORT const int Private;
25 | extern PHPCPP_EXPORT const int Const;
26 |
27 | /**
28 | * Modifiers that are supported for methods and properties
29 | */
30 | extern PHPCPP_EXPORT const int MethodModifiers;
31 | extern PHPCPP_EXPORT const int PropertyModifiers;
32 |
33 | /**
34 | * End namespace
35 | */
36 | }
37 |
--------------------------------------------------------------------------------
/include/noexcept.h:
--------------------------------------------------------------------------------
1 | /**
2 | * NoExcept.h
3 | *
4 | * Some compilers (hello microsoft!) do not yet support the "noexcept"
5 | * keyword. To overcome this, we use a macro that expands to "noexcept"
6 | *
7 | * @author Emiel Bruijntjes
8 | * @author atvive
9 | */
10 |
11 | /**
12 | * Macro to be able to support MSVC compiler
13 | */
14 | #ifndef _NOEXCEPT
15 | # ifndef _MSC_VER
16 | # define _NOEXCEPT noexcept
17 | # else
18 | # define _NOEXCEPT __declspec(nothrow)
19 | # endif
20 | #endif
21 |
22 |
--------------------------------------------------------------------------------
/include/parameters.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Parameters.h
3 | *
4 | * Wrapper around parameters that are passed to a
5 |
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Forward declarations
17 | */
18 | class Base;
19 |
20 | /**
21 | * Class definition
22 | */
23 | class PHPCPP_EXPORT Parameters : public std::vector
24 | {
25 | private:
26 | /**
27 | * The base object
28 | * @var Base
29 | */
30 | Base *_object = nullptr;
31 |
32 | protected:
33 | /**
34 | * Protected constructor
35 | *
36 | * The constructor is protected because extension programmers are not
37 | * supposed to instantiate parameters objects themselves
38 | *
39 | * @param object The 'this' object
40 | */
41 | Parameters(Base *object) : _object(object) {}
42 |
43 | public:
44 | /**
45 | * Do _not_ add a virtual destructor here.
46 | *
47 | * We are extending a vector, which does not itself
48 | * have a virtual destructor, so destructing through
49 | * a pointer to this vector has no effect.
50 | *
51 | * By adding a virtual destructor we create a vtable,
52 | * which makes the class bigger, causing slicing and
53 | * then we are actually introducing the problem that
54 | * we are trying to avoid!
55 | */
56 |
57 | /**
58 | * The object that is being called
59 | * @return Base
60 | */
61 | Base *object() const
62 | {
63 | return _object;
64 | }
65 | };
66 |
67 | /**
68 | * End of namespace
69 | */
70 | }
71 |
--------------------------------------------------------------------------------
/include/platform.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Platform.h
3 | *
4 | * Macro that we use to find out whether we run on 64bit or 32bit
5 | * platforms.
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2015 Copernica BV
9 | */
10 |
11 | // Check windows
12 | #if _WIN32 || _WIN64
13 | #if _WIN64
14 | #define PHPCPP_64BIT
15 | #else
16 | #define PHPCPP_32BIT
17 | #endif
18 | #endif
19 |
20 | // Check GCC and clang
21 | #if __GNUC__ || __clang__
22 | #if __x86_64__ || __ppc64__
23 | #define PHPCPP_64BIT
24 | #else
25 | #define PHPCPP_32BIT
26 | #endif
27 | #endif
28 |
--------------------------------------------------------------------------------
/include/script.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Script.h
3 | *
4 | * Class that can be used to evaluate a PHP script in the current PHP context.
5 | *
6 | * The difference between directly calling eval() is that the script object
7 | * will first evaluate the string, and then it can be executed multiple times.
8 | *
9 | * @author Emiel Bruijntjes
10 | * @copyright 2014 Copernica BV
11 | */
12 |
13 | /**
14 | * Forward declarations
15 | */
16 | struct _zend_op_array;
17 |
18 | /**
19 | * Set up namespace
20 | */
21 | namespace Php {
22 |
23 | /**
24 | * Forward declarations
25 | */
26 | class Opcodes;
27 |
28 | /**
29 | * Class definition
30 | */
31 | class PHPCPP_EXPORT Script
32 | {
33 | public:
34 | /**
35 | * Constructor
36 | *
37 | * The constructor will not throw any exceptions, even when invalid
38 | * PHP code is passed to it that can not be evaluated. You should call
39 | * the valid() to find out if the script was valid (could be parsed).
40 | *
41 | * @param name Name of the PHP script
42 | * @param source PHP source code to be evaluated
43 | * @param size Length of the source code
44 | */
45 | Script(const char *name, const char *source, size_t size) _NOEXCEPT;
46 |
47 | /**
48 | * Alternative constructor without a size
49 | * @param name Name of the PHP script
50 | * @param source PHP source code to be evaluated
51 | */
52 | Script(const char *name, const char *source) _NOEXCEPT : Script(name, source, ::strlen(source)) {}
53 |
54 | /**
55 | * Alternative constructor without a name
56 | * @param source PHP source code to be evaluated
57 | * @param size Length of the source code
58 | */
59 | Script(const char *source, size_t size) _NOEXCEPT : Script("Unknown", source, size) {}
60 |
61 | /**
62 | * Alternative constructor without a name and without a size
63 | * @param source PHP source code to be evaluated
64 | */
65 | Script(const char *source) _NOEXCEPT : Script("Unknown", source, ::strlen(source)) {}
66 |
67 | /**
68 | * Constructor based on a std::string
69 | * @param source PHP source code to be evaluated
70 | */
71 | Script(const std::string &source) _NOEXCEPT : Script("Unknown", source.c_str(), source.size()) {}
72 |
73 | /**
74 | * Destructor
75 | */
76 | virtual ~Script();
77 |
78 | /**
79 | * Is the script a valid PHP script without syntax errors?
80 | * @return bool
81 | */
82 | bool valid() const;
83 |
84 | /**
85 | * Execute the script
86 | * The return value of the script is returned
87 | * @return Value
88 | */
89 | Value execute() const;
90 |
91 | private:
92 | /**
93 | * The opcodes
94 | * @var Opcodes
95 | */
96 | Opcodes *_opcodes;
97 |
98 | /**
99 | * Helper function to compile the source code
100 | * @param name name of the script
101 | * @param script actual PHP code
102 | * @param size length of the string
103 | * @return opcodes
104 | */
105 | static struct _zend_op_array *compile(const char *name, const char *phpcode, size_t size);
106 |
107 | };
108 |
109 | /**
110 | * End of namespace
111 | */
112 | }
113 |
--------------------------------------------------------------------------------
/include/serializable.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Serializable interface
3 | *
4 | * This interface can be implemented to make an object that can be passed to
5 | * the PHP serialize() and unserialize() methods.
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2014 Copernica BV
9 | */
10 |
11 | /**
12 | * Set up namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * Class definition
18 | */
19 | class PHPCPP_EXPORT Serializable
20 | {
21 | public:
22 | /**
23 | * Method to serialize the object
24 | *
25 | * This method should return a string representation of the object that
26 | * can be passed to the unserialize() method and that will revive the object
27 | *
28 | * @return std::string
29 | */
30 | virtual std::string serialize() = 0;
31 |
32 | /**
33 | * Unserialize the object
34 | *
35 | * This method is called as an alternative __construct() method to initialize
36 | * the object. The passed in string parameter in in the format earlier returned
37 | * by a call to serialize()
38 | *
39 | * @param input String to parse
40 | * @param size Size of the string
41 | */
42 | virtual void unserialize(const char *input, size_t size) = 0;
43 | };
44 |
45 | /**
46 | * End namespace
47 | */
48 | }
49 |
--------------------------------------------------------------------------------
/include/stream.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Stream.h
3 | *
4 | * PHP internally uses a "stream" concept. This is a complicated
5 | * abstraction layer for sockets and filedescriptors and other sources
6 | * from which data can be read and/or to which data can be sent.
7 | *
8 | * This Php::Stream class can be used to wrap around a value (if that
9 | * value contains a stream) to access stream-specific properties. This
10 | * class currently has a very limited API, but more features can be
11 | * added if needed.
12 | *
13 | * @author Emiel Bruijntjes
14 | * @copyright 2019 Copernica BV
15 | */
16 |
17 | /**
18 | * Include guard
19 | */
20 | #pragma once
21 |
22 | /**
23 | * Forward declaration
24 | */
25 | struct _php_stream;
26 |
27 | /**
28 | * Begin of namespace
29 | */
30 | namespace Php {
31 |
32 | /**
33 | * Class definition
34 | */
35 | class PHPCPP_EXPORT Stream
36 | {
37 | private:
38 | /**
39 | * The wrapped value that is suppoed to hold a stream
40 | * @var Value
41 | */
42 | Value _value;
43 |
44 | /**
45 | * Object holding the stream data
46 | * @var struct _php_stream
47 | */
48 | struct _php_stream *_stream;
49 |
50 | public:
51 | /**
52 | * Constructor
53 | * @param value value to be wrapped
54 | * @throws std::runtime_error
55 | */
56 | Stream(const Value &value);
57 |
58 | /**
59 | * No copying
60 | * @param that
61 | */
62 | Stream(const Stream &that) = delete;
63 |
64 | /**
65 | * Destructor
66 | */
67 | virtual ~Stream();
68 |
69 | /**
70 | * Size of the read-buffer (number of bytes that have already been read
71 | * from the underlying filesystem, but that are not yet passed to php space)
72 | * @return size_t
73 | */
74 | size_t readbuffer() const;
75 |
76 | /**
77 | * Get access to the internal filedescriptor
78 | * Note that not every stream is always associated with a filedescriptor,
79 | * in which case this function returns -1
80 | * @return int
81 | */
82 | int fd() const;
83 | };
84 |
85 | /**
86 | * End of namespace
87 | */
88 | }
89 |
90 |
--------------------------------------------------------------------------------
/include/streams.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Streams.h
3 | *
4 | * Just like the standard std::cout and std::cerr objects to output data, you
5 | * can use similar stream objects for outputting data to PHP. Php::out is the
6 | * C++ equivalent of the PHP echo() function, and Php::err() is the C++ stream
7 | * that behaves like calling trigger_error() from PHP.
8 | *
9 | * Php::out << "this is example text" << std::endl;
10 | * Php::err << "this is an error message" << std::endl;
11 | *
12 | * @author Emiel Bruijntjes
13 | * @copyright 2014 Copernica BV
14 | */
15 |
16 | /**
17 | * Set up namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Define the out and err objects
23 | */
24 | extern thread_local PHPCPP_EXPORT std::ostream out;
25 | extern thread_local PHPCPP_EXPORT std::ostream error;
26 | extern thread_local PHPCPP_EXPORT std::ostream notice;
27 | extern thread_local PHPCPP_EXPORT std::ostream warning;
28 | extern thread_local PHPCPP_EXPORT std::ostream deprecated;
29 |
30 | /**
31 | * End namespace
32 | */
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/include/super.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Super.h
3 | *
4 | * The Super class is used to implement one of the super variables $_POST,
5 | * $_GET, $_SERVER, et cetera
6 | *
7 | * @copyright 2014 Copernica BV
8 | * @author Emiel Bruijntjes
9 | */
10 |
11 | /**
12 | * Set up namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * Class definition
18 | */
19 | class PHPCPP_EXPORT Super
20 | {
21 | public:
22 | /**
23 | * Constructor
24 | *
25 | * Extension writers do not have to access the super-globals themselves.
26 | * They are always accessible via Php::POST, Php::GET, et cetera.
27 | *
28 | * @param index index number
29 | * @param name name of the variable in PHP
30 | */
31 | Super(int index, const char *name) : _index(index), _name(name) {}
32 |
33 | /**
34 | * Destructor
35 | */
36 | virtual ~Super() {}
37 |
38 | /**
39 | * Array access operator
40 | * This can be used for accessing associative arrays
41 | * @param key
42 | * @return Value
43 | */
44 | Value operator[](const std::string &key)
45 | {
46 | // convert object to a value object, and retrieve the key
47 | return value().get(key);
48 | }
49 |
50 | /**
51 | * Array access operator
52 | * This can be used for accessing associative arrays
53 | * @param key
54 | * @return Value
55 | */
56 | Value operator[](const char *key)
57 | {
58 | // convert object to a value object, and retrieve the key
59 | return value().get(key);
60 | }
61 |
62 | /**
63 | * Casting operator to cast to a value object
64 | * @return Value
65 | */
66 | operator Value ()
67 | {
68 | // we have a private function for this
69 | return value();
70 | }
71 |
72 | /**
73 | * Return an iterator for iterating over the variables
74 | * @return iterator
75 | */
76 | ValueIterator begin()
77 | {
78 | // convert to value, and call begin on the value object
79 | return value().begin();
80 | }
81 |
82 | /**
83 | * Return an iterator for iterating over the variables
84 | * @return iterator
85 | */
86 | ValueIterator end()
87 | {
88 | // convert to value, and call end on that object
89 | return value().end();
90 | }
91 |
92 | private:
93 | /**
94 | * Index number
95 | * @var int
96 | */
97 | int _index;
98 |
99 | /**
100 | * Name of the variable in PHP
101 | * @var name
102 | */
103 | const char *_name;
104 |
105 | /**
106 | * Turn the object into a value object
107 | * @return Value
108 | */
109 | Value value();
110 |
111 | };
112 |
113 | /**
114 | * A number of super-globals are always accessible
115 | */
116 | extern PHPCPP_EXPORT Super POST;
117 | extern PHPCPP_EXPORT Super GET;
118 | extern PHPCPP_EXPORT Super COOKIE;
119 | extern PHPCPP_EXPORT Super SERVER;
120 | extern PHPCPP_EXPORT Super ENV;
121 | extern PHPCPP_EXPORT Super FILES;
122 | extern PHPCPP_EXPORT Super REQUEST;
123 |
124 | /**
125 | * End namespace
126 | */
127 | }
128 |
--------------------------------------------------------------------------------
/include/thread_local.h:
--------------------------------------------------------------------------------
1 | /**
2 | * thread_local.h
3 | *
4 | * Some compilers claim to be C++11 compliant, yet fail to
5 | * implement basic features, such as the thread_local storage
6 | * specifier. These little cripples we help out a bit by
7 | * redefining the keyword to whatever proprietary cruft
8 | * they are using.
9 | *
10 | * @copyright 2016 Copernica B.V.
11 | */
12 |
13 | // are we dealing with an outdated sort of compiler?
14 | #if defined(_MSC_VER) && _MSC_VER < 1500
15 | // can't use thread_local, but we can use their
16 | // limited storage scope specifier in this case
17 | #define thread_local __declspec( thread )
18 | #endif
19 |
--------------------------------------------------------------------------------
/include/throwable.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Throwable.h
3 | *
4 | * Base class for exceptions and errors, where an error is a runtime
5 | * problem with the program (like file-does-not-exist), and an error
6 | * is a more fatal programming problem (like call-to-private-method).
7 | *
8 | * @author Jasper van Eck
9 | * @author Emiel Bruijntjes
10 | * @copyright 2013 - 2023 Copernica BV
11 | */
12 | #include
13 | #include
14 |
15 | /**
16 | * Forward declarations
17 | */
18 | struct _zend_object;
19 |
20 | /**
21 | * Set up namespace
22 | */
23 | namespace Php {
24 |
25 | /**
26 | * Class definition
27 | */
28 | class PHPCPP_EXPORT Throwable : public std::runtime_error
29 | {
30 | protected:
31 | /**
32 | * The exception code
33 | * @var long int
34 | */
35 | long int _code = -1;
36 |
37 |
38 | protected:
39 | /**
40 | * Protected constructor - only derived classes can instantiate
41 | * @param message The exception message
42 | */
43 | Throwable(const std::string &message) : std::runtime_error(message) {}
44 |
45 | /**
46 | * Another protected constructor
47 | * @param object
48 | */
49 | Throwable(struct _zend_object *object);
50 |
51 | public:
52 | /**
53 | * Destructor
54 | */
55 | virtual ~Throwable() = default;
56 |
57 | /**
58 | * Rethrow the exception / make sure that it ends up in PHP space
59 | */
60 | virtual void rethrow() = 0;
61 |
62 | /**
63 | * Returns the exception code
64 | * @return The exception code
65 | */
66 | long int code() const _NOEXCEPT
67 | {
68 | // expose the code
69 | return _code;
70 | }
71 | };
72 |
73 | /**
74 | * End of namespace
75 | */
76 | }
77 |
--------------------------------------------------------------------------------
/include/traversable.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Traversable.h
3 | *
4 | * Interface that should be implemented by classes that want to be
5 | * traversable. A traversable objects can be iterated over in a PHP
6 | * foreach loop.
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2014 Copernica BV
10 | */
11 |
12 | /**
13 | * Set up namespace
14 | */
15 | namespace Php {
16 |
17 | /**
18 | * Class definition
19 | */
20 | class PHPCPP_EXPORT Traversable
21 | {
22 | public:
23 | /**
24 | * Retrieve an instance of the iterator
25 | * @return Iterator
26 | */
27 | virtual Iterator *getIterator() = 0;
28 | };
29 |
30 | /**
31 | * End namespace
32 | */
33 | }
34 |
--------------------------------------------------------------------------------
/include/type.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Type.h
3 | *
4 | * In this file an enumeration type is defined with all supported variable
5 | * types.
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2013 Copernica BV
9 | */
10 |
11 | /**
12 | * Set up namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * Supported types for variables
18 | * The values are the same as the ones used internally in Zend
19 | */
20 | enum class Type : unsigned char {
21 | Undefined = 0, // Variable is not set
22 | Null = 1, // Null will allow any type
23 | False = 2, // Boolean false
24 | True = 3, // Boolean true
25 | Numeric = 4, // Integer type
26 | Float = 5, // Floating point type
27 | String = 6, // A string obviously
28 | Array = 7, // An array of things
29 | Object = 8, // An object
30 | Resource = 9, // A resource
31 | Reference = 10, // Reference to another value (can be any type!)
32 | Constant = 11, // A constant value
33 | ConstantAST = 12, // I think an Abstract Syntax tree, not quite sure
34 |
35 | // "fake types", not quite sure what that means
36 | Bool = 13, // You will never get this back as a type
37 | Callable = 14, // I don't know why this is a "fake" type
38 | };
39 |
40 | /**
41 | * End of namespace
42 | */
43 | }
44 |
--------------------------------------------------------------------------------
/include/valueiterator.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ValueIterator.h
3 | *
4 | * This is an internal helper class that is used when iterating over a
5 | * Php::Value object - stl style.
6 | *
7 | * Thus, when you do c++ things like "for (auto &iter : value)", internally
8 | * a ValueIterator object is being used.
9 | *
10 | * @author Emiel Bruijntjes
11 | * @copyright 2014 Copernica BV
12 | */
13 |
14 | /**
15 | * Set up namespace
16 | */
17 | namespace Php {
18 |
19 | /**
20 | * Forward declarations
21 | */
22 | class ValueIteratorImpl;
23 |
24 | /**
25 | * Class definition
26 | */
27 | class PHPCPP_EXPORT ValueIterator
28 | {
29 | public:
30 | /**
31 | * Constructor
32 | * @param impl Implementation iterator
33 | */
34 | ValueIterator(ValueIteratorImpl *impl);
35 |
36 | /**
37 | * Copy constructor
38 | * @param that
39 | */
40 | ValueIterator(const ValueIterator &that);
41 |
42 | /**
43 | * Destructor
44 | */
45 | virtual ~ValueIterator();
46 |
47 | /**
48 | * Increment position (pre-increment)
49 | * @return ValueIterator
50 | */
51 | ValueIterator &operator++();
52 |
53 | /**
54 | * Increment position (post-increment)
55 | * @return ValueIterator
56 | */
57 | ValueIterator operator++(int)
58 | {
59 | // make a copy
60 | ValueIterator copy(*this);
61 |
62 | // increment current object
63 | ++(*this);
64 |
65 | // and return the unchanged original
66 | return copy;
67 | }
68 |
69 | /**
70 | * Decrement position (pre-decrement)
71 | * @return ValueIterator
72 | */
73 | ValueIterator &operator--();
74 |
75 | /**
76 | * Increment position (post-decrement)
77 | * @return ValueIterator
78 | */
79 | ValueIterator operator--(int)
80 | {
81 | // make a copy
82 | ValueIterator copy(*this);
83 |
84 | // decrement current object
85 | --(*this);
86 |
87 | // and return the unchanged original
88 | return copy;
89 | }
90 |
91 | /**
92 | * Compare with other iterator
93 | * @param that
94 | * @return bool
95 | */
96 | bool operator==(const ValueIterator &that) const;
97 |
98 | /**
99 | * Compare with other iterator
100 | * @param that
101 | * @return bool
102 | */
103 | bool operator!=(const ValueIterator &that) const;
104 |
105 | /**
106 | * Dereference, this returns a std::pair with the current key and value
107 | * @return std::pair
108 | */
109 | const std::pair &operator*() const;
110 |
111 | /**
112 | * Dereference, this returns a std::pair with the current key and value
113 | * @return std::pair
114 | */
115 | const std::pair *operator->() const;
116 |
117 | private:
118 | /**
119 | * Pointer to the actual implementation
120 | * @var std::unique_ptr
121 | */
122 | std::unique_ptr _impl;
123 |
124 | };
125 |
126 | /**
127 | * End namespace
128 | */
129 | }
130 |
--------------------------------------------------------------------------------
/include/version.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Version.h
3 | *
4 | * Macro with API version. The API version number prevents that
5 | * extensions are loaded that are incompatible with the libphpcpp.so
6 | * library
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2015 Copernica BV
10 | */
11 |
12 | /**
13 | * Macro with version number (this is incremented with every release)
14 | */
15 | #define PHPCPP_API_VERSION 20150126
16 |
--------------------------------------------------------------------------------
/include/visibility.h:
--------------------------------------------------------------------------------
1 | /**
2 | * visibility.h
3 | *
4 | * This file defines macros used to define whether a symbol
5 | * should be exported. It is only used for classes and
6 | * functions that are defined in the public API to reduce
7 | * the size of the symbol table, make linking and loading
8 | * of the PHP-CPP library faster and generate more
9 | * optimized code as a result.
10 | *
11 | * @copyright 2015 Copernica B.V.
12 | */
13 |
14 | #if defined _WIN32 || defined __CYGWIN__
15 | #ifdef BUILDING_PHPCPP
16 | #ifdef __GNUC__
17 | #define PHPCPP_EXPORT __attribute__ ((dllexport))
18 | #else
19 | #define PHPCPP_EXPORT __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
20 | #endif
21 | #else
22 | #ifdef __GNUC__
23 | #define DLL_EXPORT __attribute__ ((dllimport))
24 | #else
25 | #define DLL_EXPORT __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
26 | #endif
27 | #endif
28 | #else
29 | #define PHPCPP_EXPORT __attribute__ ((visibility ("default")))
30 | #endif
31 |
--------------------------------------------------------------------------------
/include/zval.h:
--------------------------------------------------------------------------------
1 | /**
2 | * zval.h
3 | *
4 | * A buffer to have a zval on the stack without
5 | * needing to do any heap allocations
6 | *
7 | * @copyright 2016 Copernica B.V.
8 | */
9 |
10 | /**
11 | * Include guard
12 | */
13 | #pragma once
14 |
15 | /**
16 | * Dependencies
17 | */
18 | #include
19 |
20 | /**
21 | * Forward declarations
22 | */
23 | struct _zval_struct;
24 |
25 | /**
26 | * Typedef for zval aligned storage
27 | */
28 | using aligned_zval_struct = typename std::aligned_storage<16>::type;
29 |
30 | /**
31 | * Start namespace
32 | */
33 | namespace Php
34 | {
35 |
36 | /**
37 | * The zend value wrapper
38 | */
39 | class Zval
40 | {
41 | private:
42 | /**
43 | * The zval buffer
44 | * @var aligned_zval_struct
45 | */
46 | aligned_zval_struct _buffer;
47 |
48 | public:
49 | /**
50 | * Cast to a zval
51 | *
52 | * @return zend value structure
53 | */
54 | operator _zval_struct * () const &
55 | {
56 | // reinterpret the value as a zval
57 | return const_cast(reinterpret_cast(&_buffer));
58 | }
59 |
60 | /**
61 | * Cast to a zval
62 | *
63 | * @return zend value structure
64 | */
65 | struct _zval_struct &operator* () const &
66 | {
67 | // reinterpret and dereference
68 | return *const_cast(reinterpret_cast(&_buffer));
69 | }
70 |
71 | /**
72 | * In case this is a reference, dereference it into a zval.. In case it isn't
73 | * the actual value is returned.
74 | * @return struct _zval_struct*
75 | */
76 | struct _zval_struct *dereference() const;
77 | };
78 |
79 | /**
80 | * End namespace
81 | */
82 | }
83 |
--------------------------------------------------------------------------------
/phpcpp.h:
--------------------------------------------------------------------------------
1 | /**
2 | * phpcpp.h
3 | *
4 | * Library to build PHP extensions with CPP
5 | *
6 | * @copyright 2013 - 2019 Copernica BV
7 | * @author Emiel Bruijntjes
8 | */
9 |
10 | #ifndef PHPCPP_H
11 | #define PHPCPP_H
12 |
13 | /**
14 | * Other C and C++ libraries that PhpCpp depends on
15 | */
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include