diff options
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.cpp | 3 | ||||
-rw-r--r-- | Examples/CppClassesInPhp/cppclassinphp.php | 8 | ||||
-rw-r--r-- | include/base.h | 22 | ||||
-rw-r--r-- | include/classbase.h | 17 | ||||
-rw-r--r-- | src/base.cpp | 6 | ||||
-rw-r--r-- | src/classbase.cpp | 2 |
6 files changed, 43 insertions, 15 deletions
diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp index 23f86bf..741a44d 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.cpp +++ b/Examples/CppClassesInPhp/cppclassinphp.cpp @@ -46,6 +46,8 @@ public: std::cout << "myMethod is called." << std::endl; _x = params[0]; + + std::cout << "get property1 " << value()["property1"] << std::endl; } }; @@ -76,7 +78,6 @@ extern "C" customClass.method("myMethod2", &MyCustomClass::myMethod); customClass.property("property1", "prop1"); customClass.property("property2", "prop2", Php::Protected); - customClass.method("myAbstract"); // add the class to the extension extension.add(customClass); diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php index c2076d3..7c06675 100644 --- a/Examples/CppClassesInPhp/cppclassinphp.php +++ b/Examples/CppClassesInPhp/cppclassinphp.php @@ -9,14 +9,6 @@ //create a MyCustomClass object, which is an object of a C++ class $object = new MyClass(); -class ImplementedInterface implements MyInterface -{ -} - -$object2 = new ImplementedInterface(); - -return; - // run a function of the class $object->myMethod(1); diff --git a/include/base.h b/include/base.h index 93ee66e..04f0c0e 100644 --- a/include/base.h +++ b/include/base.h @@ -52,6 +52,28 @@ public: { return value()[name]; } + +private: + /** + * The zend_object + * @var zend_object + */ + struct _zend_object *_object = nullptr; + + /** + * Private method to assign the zend object + * @param zend_object + */ + void assign(struct _zend_object *object) + { + // copy pointer + _object = object; + } + + /** + * ClassBase has access to private data + */ + friend class ClassBase; }; diff --git a/include/classbase.h b/include/classbase.h index 249122a..a9d9ef5 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -76,6 +76,23 @@ public: * Destructor */ virtual ~ClassBase(); + + /** + * Construct a new instance of the object + * @return Base + */ + Base* construct(struct _zend_object *object) + { + // construct the base + auto *result = construct(); + if (!result) return nullptr; + + // assign the zend object to it + result->assign(object); + + // done + return result; + } /** * Construct a new instance of the object diff --git a/src/base.cpp b/src/base.cpp index b5f2f86..867fd9c 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -18,12 +18,8 @@ namespace Php { */ Value Base::value() const { - // because the object is stored in a MixedObject, we know that the zend_object - // structure is right in front of the this pointer - zend_object *object = (zend_object *)this - sizeof(zend_object); - // wrap the properties table, as a reference - return Object(*object->properties_table, true); + return Object(*_object->properties_table, true); } /** diff --git a/src/classbase.cpp b/src/classbase.cpp index 39c8790..6a679b3 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -100,7 +100,7 @@ static zend_object_value create_object(zend_class_entry *type TSRMLS_DC) result.handle = zend_objects_store_put(object, NULL, deallocate_object, clone_object TSRMLS_CC); // finally, construct the cpp object - object->cpp = info->construct(); + object->cpp = info->construct(&object->php); // done return result; |