diff --git a/documentation/properties.html b/documentation/properties.html index 288a7e6d..6f3ece57 100644 --- a/documentation/properties.html +++ b/documentation/properties.html @@ -148,11 +148,12 @@

Normal member variables

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 @@ 

Class constants

// 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 @@

Class constants

- 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 a8383415..e52bd146 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 03cce2bd..f3673b1d 100644 --- a/src/classimpl.h +++ b/src/classimpl.h @@ -351,14 +351,14 @@ class ClassImpl * @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 @@ class ClassImpl * @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 @@ class ClassImpl * @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 @@ class ClassImpl * @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 c987b8ad..37207304 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 */