summaryrefslogtreecommitdiff
path: root/zend/object.cpp
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-06 21:53:24 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-06 21:53:24 +0200
commit35fd3ccbeb4def71b4d8a59dfbb5c31201b099b9 (patch)
tree915223360aed4743aa6127fde4836aa413a260e5 /zend/object.cpp
parentda4710512865e6816585ac4ab8edab2fa125e2d8 (diff)
renamed src directory to zend directory, disabled TSRM debug code
Diffstat (limited to 'zend/object.cpp')
-rw-r--r--zend/object.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/zend/object.cpp b/zend/object.cpp
new file mode 100644
index 0000000..d7fc158
--- /dev/null
+++ b/zend/object.cpp
@@ -0,0 +1,79 @@
+/**
+ * Object.cpp
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2014 Copernica BV
+ */
+#include "includes.h"
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Constructor to create a new instance of a builtin class
+ *
+ * @param name Name of the class to instantiate
+ * @param base Implementation of the class
+ */
+Object::Object(const char *name, Base *base)
+{
+ // does the object already have a handle?
+ if (base->implementation())
+ {
+ // the object is already instantiated, we can assign it the this object
+ operator=(Value(base));
+ }
+ else
+ {
+ // we need the tsrm_ls variable
+ TSRMLS_FETCH();
+
+ // this is a brand new object that should be allocated, the C++ instance
+ // is already there (created by the extension) but it is not yet stored
+ // in PHP, find out the classname first
+ auto *entry = zend_fetch_class(name, strlen(name), 0 TSRMLS_CC);
+ if (!entry) throw Php::Exception(std::string("Unknown class name ") + name);
+
+ // construct an implementation (this will also set the implementation
+ // member in the base object)
+ new ObjectImpl(entry, base TSRMLS_CC);
+
+ // now we can store it
+ operator=(Value(base));
+ }
+}
+
+/**
+ * Internal method to instantiate an object
+ * @param name
+ */
+void Object::instantiate(const char *name)
+{
+ // we need the tsrm_ls variable
+ TSRMLS_FETCH();
+
+ // convert the name into a class_entry
+ auto *entry = zend_fetch_class(name, strlen(name), 0 TSRMLS_CC);
+ if (!entry) throw Php::Exception(std::string("Unknown class name ") + name);
+
+ // initiate the zval (which was already allocated in the base constructor)
+ object_init_ex(_val, entry);
+
+ // @todo should we call methods like allocating hashtables, copying and
+ // initializing properties, et cetera????? In all example you always
+ // see such complicated and next-to-impossible-to-understand
+ // sequences of functions being called, but this object_init_ex
+ // also seems to work...
+
+ // @todo is this a memory leak? the base class first initializes a stdClass,
+ // and then we overwrite it with a specific class
+
+}
+
+/**
+ * End namespace
+ */
+}
+