Skip to content

Commit

Permalink
implemented static properties as requested in issue #58
Browse files Browse the repository at this point in the history
  • Loading branch information
EmielBruijntjes committed Apr 5, 2014
1 parent d9c16dd commit ffdccb8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
15 changes: 10 additions & 5 deletions documentation/properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ <h2 id="normal-members">Normal member variables</h2>
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>
Expand All @@ -179,6 +180,9 @@ <h2 id="constants">Class constants</h2>
// 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));

Expand All @@ -189,7 +193,8 @@ <h2 id="constants">Class constants</h2>
</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>
Expand Down
7 changes: 7 additions & 0 deletions include/modifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ namespace Php {
/**
* The modifiers are constants
*/
extern const int Static;
extern const int Abstract;
extern const int Final;
extern const int Public;
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
*/
Expand Down
44 changes: 22 additions & 22 deletions src/classimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand All @@ -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<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
Expand All @@ -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<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
Expand All @@ -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<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
Expand Down
11 changes: 10 additions & 1 deletion src/modifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* In this file an enumeration type is with the possible
* member modifiers
*
* @author Martijn Otto
* @author Martijn Otto <[email protected]>
* @author Emiel Bruijntjes <[email protected]>
*
* @copyright 2014 Copernica BV
*/
#include "includes.h"
Expand All @@ -17,13 +19,20 @@ namespace Php {
/**
* The modifiers are constants
*/
const int Static = 0x01;
const int Abstract = 0x02;
const int Final = 0x04;
const int Public = 0x100;
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
*/
Expand Down

0 comments on commit ffdccb8

Please sign in to comment.