From 2e4318b5ecae22b6bc0f04dfc116ff3e4647b1e8 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Wed, 2 Apr 2014 23:21:16 +0200 Subject: it now is possible to register a class as base class (although not yet working) (request from issue #52) --- include/class.h | 18 +++++++++++++----- include/classbase.h | 6 +++--- src/classbase.cpp | 6 ++++++ src/classimpl.h | 11 +++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/include/class.h b/include/class.h index 8c78069..7b9a928 100644 --- a/include/class.h +++ b/include/class.h @@ -197,13 +197,13 @@ public: * * The base class that you supply must already be registered. And because * your extension is most likely registered before any user space PHP scripts - * run, you can only specify built-in classes, or classes that you created - * in your own extension. + * run, you can only specify classes that you created in your own extension. * - * @param name Name of the base class + * @param base Php::Class object * @return Class Same object to allow chaining */ -// Class &extends(const char *name) { ClassBase::extends(name); return *this; } + template + Class &extends(const Class &base) { ClassBase::extends(base); return *this; } private: /** @@ -534,9 +534,17 @@ private: } /** - * Namespaces have access to the private base class + * Namespaces have access to the private base class, so that the classes + * can be registered. */ friend class Namespace; + + /** + * All Php::Class also need access to the base class to + * register this class as base class. + */ + template friend class Class; + }; /** diff --git a/include/classbase.h b/include/classbase.h index c2a9022..a736c4e 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -245,7 +245,7 @@ protected: /** * Set property with callbacks * @param name Name of the property - * @param getter Getter method + * @param getter Getter methode * @param setter Setter method */ void property(const char *name, const getter_callback_0 &getter); @@ -263,9 +263,9 @@ protected: /** * Set the base class - * @param name Name of the base class + * @param base Php::Class object that is the base */ -// void extends(const std::string &name) { _base = name; } + void extends(const ClassBase &base); private: /** diff --git a/src/classbase.cpp b/src/classbase.cpp index c753c34..18d81d5 100644 --- a/src/classbase.cpp +++ b/src/classbase.cpp @@ -117,6 +117,12 @@ void ClassBase::property(const char *name, const getter_callback_1 &getter, cons */ void ClassBase::implements(const ClassBase &interface) { _impl->implements(interface._impl); } +/** + * Set the base class + * @param base Php::Class object that is the base + */ +void ClassBase::extends(const ClassBase &base) { _impl->extends(base._impl); } + /** * End namespace */ diff --git a/src/classimpl.h b/src/classimpl.h index 03dc548..4da4eba 100644 --- a/src/classimpl.h +++ b/src/classimpl.h @@ -78,6 +78,12 @@ private: * @var std::list */ std::list> _interfaces; + + /** + * The parent/base class + * @var std::shared_ptr + */ + std::shared_ptr _parent; /** @@ -407,6 +413,11 @@ public: */ void implements(const std::shared_ptr &interface) { _interfaces.push_back(interface); } + /** + * Set the base class + * @param base The base class + */ + void extends(const std::shared_ptr &base) { _parent = base; } }; -- cgit v1.2.3