summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-05 11:58:12 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-05 11:58:12 +0200
commitffdccb83d460791202bdb258dbb9106da877da3b (patch)
treed8e7e1cbfd90f05611c43fdd5e565b5438fb4765
parentd9c16ddf8f36fcd50c39a73f5a4ab242896e0a84 (diff)
implemented static properties as requested in issue #58v1.0
-rw-r--r--documentation/properties.html15
-rw-r--r--include/modifiers.h7
-rw-r--r--src/classimpl.h44
-rw-r--r--src/modifiers.cpp11
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.
</p>
-<h2 id="constants">Class constants</h2>
+<h2 id="static-constants">Static properties and class constants</h2>
<p>
- 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.
</p>
<p>
<pre class="language-cpp"><code>
@@ -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" {
</code></pre>
</p>
<p>
- 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.
</p>
<h2>Smart properties</h2>
<p>
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;
@@ -24,6 +25,12 @@ 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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(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<Method>(name, Abstract | flags, args)); }
+ void method(const char *name, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared<Method>(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<NullMember>(name, flags)); }
- void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
- void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
- void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
- void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared<BoolMember>(name, value, flags)); }
- void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, &value, 1, flags)); }
- void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, value, flags)); }
- void property(const char *name, const char *value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, value, strlen(value), flags)); }
- void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared<FloatMember>(name, value, flags)); }
+ void property(const char *name, std::nullptr_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NullMember> (name, flags & PropertyModifiers)); }
+ void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
+ void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
+ void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
+ void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared<BoolMember> (name, value, flags & PropertyModifiers)); }
+ void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember> (name, &value, 1, flags & PropertyModifiers)); }
+ void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember> (name, value, flags & PropertyModifiers)); }
+ void property(const char *name, const char *value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember> (name, value, strlen(value), flags & PropertyModifiers)); }
+ void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared<FloatMember> (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 <martijn.otto@copernica.com>
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ *
* @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;
@@ -25,6 +28,12 @@ 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
*/
}