Skip to content

Commit ffdccb8

Browse files
implemented static properties as requested in issue #58
1 parent d9c16dd commit ffdccb8

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

documentation/properties.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ <h2 id="normal-members">Normal member variables</h2>
148148
properties, but even that is probably not what you want, as storing
149149
data in native C++ variables is much faster.
150150
</p>
151-
<h2 id="constants">Class constants</h2>
151+
<h2 id="static-constants">Static properties and class constants</h2>
152152
<p>
153-
Class constants can be defined in a similar way as properties. The only
154-
difference is that you have to pass in the flag 'Php::Const' instead of
155-
one of the public, private or protected access modifiers.
153+
Static properties and class class constants can be defined in a similar way
154+
as properties. The only difference is that you have to pass in either the
155+
flag 'Php::Static' or 'Php::Const' instead of one of the Php::Public,
156+
Php::Private or Php::Protected access modifiers.
156157
</p>
157158
<p>
158159
<pre class="language-cpp"><code>
@@ -179,6 +180,9 @@ <h2 id="constants">Class constants</h2>
179180
// the Example class has a class constant
180181
example.property("MY_CONSTANT", "some value", Php::Const);
181182

183+
// and a public static propertie
184+
example.property("my_property", "initial value", Php::Public | Php::Static);
185+
182186
// add the class to the extension
183187
myExtension.add(std::move(example));
184188

@@ -189,7 +193,8 @@ <h2 id="constants">Class constants</h2>
189193
</code></pre>
190194
</p>
191195
<p>
192-
The class constant can be accessed from PHP scripts using Example::MY_CONSTANT.
196+
The class constant can be accessed from PHP scripts using Example::MY_CONSTANT,
197+
and the static properties with Example::$my_property.
193198
</p>
194199
<h2>Smart properties</h2>
195200
<p>

include/modifiers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ namespace Php {
1616
/**
1717
* The modifiers are constants
1818
*/
19+
extern const int Static;
1920
extern const int Abstract;
2021
extern const int Final;
2122
extern const int Public;
2223
extern const int Protected;
2324
extern const int Private;
2425
extern const int Const;
2526

27+
/**
28+
* Modifiers that are supported for methods and properties
29+
*/
30+
extern const int MethodModifiers;
31+
extern const int PropertyModifiers;
32+
2633
/**
2734
* End namespace
2835
*/

src/classimpl.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ class ClassImpl
351351
* @param flags Optional flags
352352
* @param args Description of the supported arguments
353353
*/
354-
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)); }
355-
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)); }
356-
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)); }
357-
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)); }
358-
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)); }
359-
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)); }
360-
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)); }
361-
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)); }
354+
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)); }
355+
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)); }
356+
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)); }
357+
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)); }
358+
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)); }
359+
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)); }
360+
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)); }
361+
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)); }
362362

363363
/**
364364
* Add a static method to the class
@@ -372,10 +372,10 @@ class ClassImpl
372372
* @param flags Optional flags
373373
* @param args Description of the supported arguments
374374
*/
375-
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)); }
376-
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)); }
377-
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)); }
378-
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)); }
375+
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)); }
376+
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)); }
377+
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)); }
378+
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)); }
379379

380380
/**
381381
* Add an abstract method to the class
@@ -384,7 +384,7 @@ class ClassImpl
384384
* @param flags Optional flags (like public or protected)
385385
* @param args Description of the supported arguments
386386
*/
387-
void method(const char *name, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared<Method>(name, Abstract | flags, args)); }
387+
void method(const char *name, int flags=0, const Arguments &args = {}) { _methods.push_back(std::make_shared<Method>(name, (flags & (MethodModifiers | Static)) | Abstract , args)); }
388388

389389
/**
390390
* Add a property to the class
@@ -399,15 +399,15 @@ class ClassImpl
399399
* @param value Actual property value
400400
* @param flags Optional flags
401401
*/
402-
void property(const char *name, std::nullptr_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NullMember>(name, flags)); }
403-
void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
404-
void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
405-
void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags)); }
406-
void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared<BoolMember>(name, value, flags)); }
407-
void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, &value, 1, flags)); }
408-
void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, value, flags)); }
409-
void property(const char *name, const char *value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember>(name, value, strlen(value), flags)); }
410-
void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared<FloatMember>(name, value, flags)); }
402+
void property(const char *name, std::nullptr_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NullMember> (name, flags & PropertyModifiers)); }
403+
void property(const char *name, int16_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
404+
void property(const char *name, int32_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
405+
void property(const char *name, int64_t value, int flags = Php::Public) { _members.push_back(std::make_shared<NumericMember>(name, value, flags & PropertyModifiers)); }
406+
void property(const char *name, bool value, int flags = Php::Public) { _members.push_back(std::make_shared<BoolMember> (name, value, flags & PropertyModifiers)); }
407+
void property(const char *name, char value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember> (name, &value, 1, flags & PropertyModifiers)); }
408+
void property(const char *name, const std::string &value, int flags = Php::Public) { _members.push_back(std::make_shared<StringMember> (name, value, flags & PropertyModifiers)); }
409+
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)); }
410+
void property(const char *name, double value, int flags = Php::Public) { _members.push_back(std::make_shared<FloatMember> (name, value, flags & PropertyModifiers)); }
411411

412412
/**
413413
* Set property with callbacks

src/modifiers.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* In this file an enumeration type is with the possible
55
* member modifiers
66
*
7-
* @author Martijn Otto
7+
* @author Martijn Otto <[email protected]>
8+
* @author Emiel Bruijntjes <[email protected]>
9+
*
810
* @copyright 2014 Copernica BV
911
*/
1012
#include "includes.h"
@@ -17,13 +19,20 @@ namespace Php {
1719
/**
1820
* The modifiers are constants
1921
*/
22+
const int Static = 0x01;
2023
const int Abstract = 0x02;
2124
const int Final = 0x04;
2225
const int Public = 0x100;
2326
const int Protected = 0x200;
2427
const int Private = 0x400;
2528
const int Const = 0;
2629

30+
/**
31+
* Modifiers that are supported for methods and properties
32+
*/
33+
const int MethodModifiers = Final | Public | Protected | Private;
34+
const int PropertyModifiers = Final | Public | Protected | Private | Const | Static;
35+
2736
/**
2837
* End namespace
2938
*/

0 commit comments

Comments
 (0)