diff options
-rw-r--r-- | Examples/CppClassesInPhp/Makefile | 2 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.cpp | 7 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.php | 23 | ||||
-rw-r--r-- | include/forcedvalue.h | 1 | ||||
-rw-r--r-- | include/object.h | 63 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/classbase.cpp | 2 | ||||
-rw-r--r-- | src/includes.h | 1 | ||||
-rw-r--r-- | src/object.cpp | 52 | ||||
-rw-r--r-- | src/value.cpp | 12 |
10 files changed, 151 insertions, 13 deletions
diff --git a/Examples/CppClassesInPhp/Makefile b/Examples/CppClassesInPhp/Makefile index 0260376..872cce3 100644 --- a/Examples/CppClassesInPhp/Makefile +++ b/Examples/CppClassesInPhp/Makefile @@ -4,7 +4,7 @@ CPP_FLAGS = -Wall -c -I. -g -std=c++11 PREFIX = /usr #Edit these lines to correspond with your own directories -LIBRARY_DIR = ${PREFIX}/lib/php5/20121212 +LIBRARY_DIR = ${PREFIX}/lib/php5/20090626 PHP_CONFIG_DIR = /etc/php5/cli/conf.d LD = g++ diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp index 7ef4d12..2d17e19 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.cpp +++ b/Examples/CppClassesInPhp/cppclassinphp.cpp @@ -44,13 +44,16 @@ public: return 33; } - void myMethod(Php::Parameters ¶ms) + Php::Value myMethod(Php::Parameters ¶ms) { // check number of parameters if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied"); std::cout << "myMethod is called." << std::endl; - _x = params[0]; + + // construct a new class + return Php::Object(params[0]); + // std::cout << "get property1 " << value()["property1"] << std::endl; // diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php index b535f0e..c969a4c 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.php +++ b/Examples/CppClassesInPhp/cppclassinphp.php @@ -5,13 +5,32 @@ * * An example file to show the working of using a C++ class in PHP. */ + +class TestClass +{ + public $x = 1223; + + public function __construct() + { + echo("TestClass::__construct\n"); + } + +} //create a MyCustomClass object, which is an object of a C++ class $object = new MyClass(); // run a function of the class -$object->myMethod(1); -$object->myMethod(2); +$obj = $object->myMethod("MyClass"); + +echo(get_class($obj)."\n"); +//echo($obj->format("Y-m-d")."\n"); + +//echo($obj->x."\n"); + +return; + +//$object->myMethod(2); echo($object->property1."\n"); echo($object->property2."\n"); diff --git a/include/forcedvalue.h b/include/forcedvalue.h index c6663e0..67c339e 100644 --- a/include/forcedvalue.h +++ b/include/forcedvalue.h @@ -132,7 +132,6 @@ protected: * Define for arrays and objects */ using Array = ForcedValue<Type::Array>; -using Object = ForcedValue<Type::Object>; /** * End of namespace diff --git a/include/object.h b/include/object.h new file mode 100644 index 0000000..f104478 --- /dev/null +++ b/include/object.h @@ -0,0 +1,63 @@ +/** + * Object.h + * + * Extended Value that can be used to instantiate new objects, and to turn + * Php::Base objects into regular Php::Value instances + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class Object : public ForcedValue<Type::Object> +{ +public: + /** + * Constructor for an empty stdClass object + * + * @todo check if this indeed leads to a stdClass instance + */ + Object() : ForcedValue<Type::Object>() {} + + /** + * @todo copy constructor, move constructor + */ + + /** + * Constructor to create a new instance + * + * This constructor comes in many different forms, to support all possible + * number of parameters that are passed to the constructor + * + * @param name Name of the class to instantiate + * @param arg0 Optional argument 1 + * @param arg1 Optional argument 2 + * @param arg2 Optional argument 3 + * @param arg3 Optional argument 4 + * @param arg4 Optional argument 5 + * @param arg5 Optional argument 6 + * @param arg6 Optional argument 7 + * @param arg7 Optional argument 8 + * @param arg8 Optional argument 9 + * @param arg9 Optional argument 10 + */ + Object(const char *name); + + /** + * Destructor + */ + virtual ~Object() {} + +}; + +/** + * End namespace + */ +} @@ -28,6 +28,7 @@ #include <phpcpp/type.h> #include <phpcpp/value.h> #include <phpcpp/forcedvalue.h> +#include <phpcpp/object.h> #include <phpcpp/hiddenpointer.h> #include <phpcpp/globals.h> #include <phpcpp/argument.h> diff --git a/src/classbase.cpp b/src/classbase.cpp index c7ade6c..af707c4 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -57,7 +57,7 @@ static zend_object_value create_object(zend_class_entry *type TSRMLS_DC) // allocate memory for the object MixedObject *object = (MixedObject *)emalloc(sizeof(MixedObject)); - // find base object + // find base object (because the class may have been extended in user space) zend_class_entry *base = type; while (base->parent) base = base->parent; diff --git a/src/includes.h b/src/includes.h index 59ee8b3..072fab2 100644 --- a/src/includes.h +++ b/src/includes.h @@ -47,6 +47,7 @@ #include "../include/type.h" #include "../include/value.h" #include "../include/forcedvalue.h" +#include "../include/object.h" #include "../include/hiddenpointer.h" #include "../include/globals.h" #include "../include/argument.h" diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 0000000..99de4a0 --- /dev/null +++ b/src/object.cpp @@ -0,0 +1,52 @@ +/** + * Object.cpp + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Constructor + * @param name + */ +Object::Object(const char *name) +{ + // step 1: convert the name into a class_entry + auto *entry = zend_fetch_class(name, strlen(name), 0); + if (!entry) throw Php::Exception("Unknown class name"); + + // initiate the zval (which was already allocated in the base constructor) + object_init_ex(_val, entry); + +// // is there a special function to create the object? +// if (entry->create_object) +// { +// // create the object +// zend_object_value value = entry->create_object(entry); +// +// // wrap this in the zval (which was already allocated in the base constructor) +// Z_TYPE_P(_val) = IS_OBJECT; +// Z_OBJVAL_P(_val) = value; +// } +// else +// { +// } + + // @todo should we call methods like allocating hashtables, copyint and + // initializing properties, et cetera????? + + // call the constructor + call("__construct"); +} + +/** + * End namespace + */ +} + diff --git a/src/value.cpp b/src/value.cpp index f7eff56..ab4c350 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1397,8 +1397,8 @@ bool Value::contains(const char *key, int size) const // retrieve the class entry auto *entry = zend_get_class_entry(_val); - // read the property - zval *property = zend_read_property(entry, _val, key, size, 0); + // read the property (cast necessary for php 5.3) + zval *property = zend_read_property(entry, _val, (char *)key, size, 0); // check if valid return property != nullptr; @@ -1461,8 +1461,8 @@ Value Value::get(const char *key, int size) const // retrieve the class entry auto *entry = zend_get_class_entry(_val); - // read the property - zval *property = zend_read_property(entry, _val, key, size, 1); + // read the property (case necessary for php 5.3) + zval *property = zend_read_property(entry, _val, (char *)key, size, 1); // wrap in value return Value(property); @@ -1534,8 +1534,8 @@ const Value &Value::set(const char *key, int size, const Value &value) // retrieve the class entry auto *entry = zend_get_class_entry(_val); - // update the property - zend_update_property(entry, _val, key, size, value._val); + // update the property (cast necessary for php 5.3) + zend_update_property(entry, _val, (char *)key, size, value._val); } else { |