summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/base.h7
-rw-r--r--include/class.h39
-rw-r--r--include/classbase.h4
-rw-r--r--src/classbase.cpp3
4 files changed, 39 insertions, 14 deletions
diff --git a/include/base.h b/include/base.h
index 2d5b815..97b153d 100644
--- a/include/base.h
+++ b/include/base.h
@@ -118,6 +118,13 @@ public:
void __destruct() const;
/**
+ * Overridable method that is called right after an object is cloned
+ *
+ * The default implementation does nothing
+ */
+ void __clone() const {}
+
+ /**
* Overridable method that is called to check if a property is set
*
* The default implementation does nothing, and the script will fall back
diff --git a/include/class.h b/include/class.h
index 23c39e1..f8ba578 100644
--- a/include/class.h
+++ b/include/class.h
@@ -255,6 +255,32 @@ private:
}
/**
+ * Call the __clone method
+ * @param base
+ */
+ virtual void callClone(Base *base) const
+ {
+ // cast to the user object
+ T *object = (T *)base;
+
+ // call the method on the base object
+ return object->__clone();
+ }
+
+ /**
+ * Call the __destruct method
+ * @param base
+ */
+ virtual void callDestruct(Base *base) const
+ {
+ // cast to the user object
+ T *object = (T *)base;
+
+ // call the method on the base object
+ return object->__destruct();
+ }
+
+ /**
* Call a method
* @param base Object to call on
* @param name Name of the method
@@ -322,19 +348,6 @@ private:
}
/**
- * Call the __destruct method
- * @param object
- */
- virtual void callDestruct(Base *base) const override
- {
- // cast to actual object
- T *obj = (T *)base;
-
- // pass on
- obj->__destruct();
- }
-
- /**
* Call a the __callStatic() function
* @param name Name of the function
* @param params Parameters passed to the function
diff --git a/include/classbase.h b/include/classbase.h
index 40f3fda..6a71ecb 100644
--- a/include/classbase.h
+++ b/include/classbase.h
@@ -161,8 +161,10 @@ protected:
virtual int callCompare(Base *object1, Base *object2) const { return 1; }
/**
- * Call the __destruct method
+ * Call the __clone and __destruct magic methods
+ * @param base
*/
+ virtual void callClone(Base *base) const {}
virtual void callDestruct(Base *base) const {}
/**
diff --git a/src/classbase.cpp b/src/classbase.cpp
index ea24cfd..0de62dd 100644
--- a/src/classbase.cpp
+++ b/src/classbase.cpp
@@ -555,6 +555,9 @@ zend_object_value ClassBase::cloneObject(zval *val TSRMLS_DC)
// had registered that as a visible method)
zend_objects_clone_members(&new_object->php, result, &old_object->php, Z_OBJ_HANDLE_P(val));
+ // was a custom clone method installed? If not we call the magic c++ __clone method
+ if (!entry->clone) meta->callClone(cpp);
+
// done
return result;
}