From ffdccb83d460791202bdb258dbb9106da877da3b Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 5 Apr 2014 11:58:12 +0200 Subject: implemented static properties as requested in issue #58 --- documentation/properties.html | 15 ++++++++++----- include/modifiers.h | 7 +++++++ src/classimpl.h | 44 +++++++++++++++++++++---------------------- src/modifiers.cpp | 11 ++++++++++- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/documentation/properties.html b/documentation/properties.html index 288a7e6..6f3ece5 100644 --- a/documentation/properties.html +++ b/documentation/properties.html @@ -148,11 +148,12 @@ extern "C" { properties, but even that is probably not what you want, as storing data in native C++ variables is much faster.

-

Class constants

+

Static properties and class constants

- Class constants can be defined in a similar way as properties. The only - difference is that you have to pass in the flag 'Php::Const' instead of - one of the public, private or protected access modifiers. + Static properties and class class constants can be defined in a similar way + as properties. The only difference is that you have to pass in either the + flag 'Php::Static' or 'Php::Const' instead of one of the Php::Public, + Php::Private or Php::Protected access modifiers.


@@ -179,6 +180,9 @@ extern "C" {
         // the Example class has a class constant
         example.property("MY_CONSTANT", "some value", Php::Const);
         
+        // and a public static propertie
+        example.property("my_property", "initial value", Php::Public | Php::Static);
+        
         // add the class to the extension
         myExtension.add(std::move(example));
         
@@ -189,7 +193,8 @@ extern "C" {
 

- The class constant can be accessed from PHP scripts using Example::MY_CONSTANT. + The class constant can be accessed from PHP scripts using Example::MY_CONSTANT, + and the static properties with Example::$my_property.

Smart properties

diff --git a/include/modifiers.h b/include/modifiers.h index a838341..e52bd14 100644 --- a/include/modifiers.h +++ b/include/modifiers.h @@ -16,6 +16,7 @@ namespace Php { /** * The modifiers are constants */ +extern const int Static; extern const int Abstract; extern const int Final; extern const int Public; @@ -23,6 +24,12 @@ extern const int Protected; extern const int Private; extern const int Const; +/** + * Modifiers that are supported for methods and properties + */ +extern const int MethodModifiers; +extern const int PropertyModifiers; + /** * End namespace */ diff --git a/src/classimpl.h b/src/classimpl.h index 03cce2b..f3673b1 100644 --- a/src/classimpl.h +++ b/src/classimpl.h @@ -351,14 +351,14 @@ public: * @param flags Optional flags * @param args Description of the supported arguments */ - void method(const char *name, const method_callback_0 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_1 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_2 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_3 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_4 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_5 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_6 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } - void method(const char *name, const method_callback_7 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags, args)); } + void method(const char *name, const method_callback_0 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_1 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_2 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_3 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_4 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_5 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_6 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } + void method(const char *name, const method_callback_7 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags & MethodModifiers, args)); } /** * Add a static method to the class @@ -372,10 +372,10 @@ public: * @param flags Optional flags * @param args Description of the supported arguments */ - void method(const char *name, const native_callback_0 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags | ZEND_ACC_STATIC, args)); } - void method(const char *name, const native_callback_1 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags | ZEND_ACC_STATIC, args)); } - void method(const char *name, const native_callback_2 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags | ZEND_ACC_STATIC, args)); } - void method(const char *name, const native_callback_3 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, flags | ZEND_ACC_STATIC, args)); } + void method(const char *name, const native_callback_0 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, (flags & MethodModifiers) | Static, args)); } + void method(const char *name, const native_callback_1 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, (flags & MethodModifiers) | Static, args)); } + void method(const char *name, const native_callback_2 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, (flags & MethodModifiers) | Static, args)); } + void method(const char *name, const native_callback_3 &method, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, method, (flags & MethodModifiers) | Static, args)); } /** * Add an abstract method to the class @@ -384,7 +384,7 @@ public: * @param flags Optional flags (like public or protected) * @param args Description of the supported arguments */ - void method(const char *name, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, Abstract | flags, args)); } + void method(const char *name, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared(name, (flags & (MethodModifiers | Static)) | Abstract , args)); } /** * Add a property to the class @@ -399,15 +399,15 @@ public: * @param value Actual property value * @param flags Optional flags */ - void property(const char *name, std::nullptr_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, flags)); } - void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } - void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } - void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } - void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } - void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared(name, &value, 1, flags)); } - void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } - void property(const char *name, const char *value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, strlen(value), flags)); } - void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags)); } + void property(const char *name, std::nullptr_t value, int flags = Php::Public) { _members.push_back(std::make_shared (name, flags & PropertyModifiers)); } + void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags & PropertyModifiers)); } + void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags & PropertyModifiers)); } + void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared(name, value, flags & PropertyModifiers)); } + void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared (name, value, flags & PropertyModifiers)); } + void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared (name, &value, 1, flags & PropertyModifiers)); } + void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared (name, value, flags & PropertyModifiers)); } + void property(const char *name, const char *value, int flags = Php::Public) { _members.push_back(std::make_shared (name, value, strlen(value), flags & PropertyModifiers)); } + void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared (name, value, flags & PropertyModifiers)); } /** * Set property with callbacks diff --git a/src/modifiers.cpp b/src/modifiers.cpp index c987b8a..3720730 100644 --- a/src/modifiers.cpp +++ b/src/modifiers.cpp @@ -4,7 +4,9 @@ * In this file an enumeration type is with the possible * member modifiers * - * @author Martijn Otto + * @author Martijn Otto + * @author Emiel Bruijntjes + * * @copyright 2014 Copernica BV */ #include "includes.h" @@ -17,6 +19,7 @@ namespace Php { /** * The modifiers are constants */ +const int Static = 0x01; const int Abstract = 0x02; const int Final = 0x04; const int Public = 0x100; @@ -24,6 +27,12 @@ const int Protected = 0x200; const int Private = 0x400; const int Const = 0; +/** + * Modifiers that are supported for methods and properties + */ +const int MethodModifiers = Final | Public | Protected | Private; +const int PropertyModifiers = Final | Public | Protected | Private | Const | Static; + /** * End namespace */ -- cgit v1.2.3