summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-25 18:38:35 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-25 18:38:35 +0100
commit3e6fb6ef89600abf2a282396384aab80ec9390ce (patch)
treefb15a9b4a5379823f787beccc0f34d6ebd8af74c
parent089c92358d4f43f1715d94244acb4d665fe50d29 (diff)
Php::Constant objects can now also be added to a Php::Class to create class constants
-rw-r--r--include/class.h27
-rw-r--r--include/classbase.h6
-rw-r--r--include/constant.h20
-rw-r--r--zend/constant.cpp26
-rw-r--r--zend/constantimpl.h46
-rw-r--r--zend/includes.h2
6 files changed, 125 insertions, 2 deletions
diff --git a/include/class.h b/include/class.h
index 2c875fa..76ac114 100644
--- a/include/class.h
+++ b/include/class.h
@@ -142,7 +142,7 @@ public:
* hidden)
*
* @param name Name of the property
- * @param value Actual property value
+ * @param value Actual default property value
* @param flags Optional flags
* @return Class Same object to allow chaining
*/
@@ -157,6 +157,31 @@ public:
Class<T> &property(const char *name, double value, int flags = Public) { ClassBase::property(name, value, flags); return *this; }
/**
+ * Create a class constant
+ *
+ * The class constant can be used in a php script as "ClassName::CONSTANT_NAME".
+ * It is a good programming practive to only use uppercase characters for
+ * constants.
+ *
+ * This is an alias for adding a class property with the "Php::Const" flag.
+ *
+ * @param name Name of the constant
+ * @param value Constant value
+ * @return Class Same object to allow chaining
+ */
+ template <typename V>
+ Class<T> &constant(const char *name, V value) { return property(name, value, Const); }
+
+ /**
+ * Add a Php::Constant to a class to use it as a class constant
+ *
+ * @param constant The constant to add
+ * @return Class Same object to allow chaining
+ */
+ Class<T> &constant(const Constant &constant) { constant.addTo(*this); return *this; }
+ Class<T> &add(const Constant &constant) { constant.addTo(*this); return *this; }
+
+ /**
* Properties as methods
*
* This is a smarter way for adding properties to a class. You can define
diff --git a/include/classbase.h b/include/classbase.h
index 1129298..a70157f 100644
--- a/include/classbase.h
+++ b/include/classbase.h
@@ -273,6 +273,12 @@ private:
* @var std::shared_ptr<ClassImpl>
*/
std::shared_ptr<ClassImpl> _impl;
+
+ /**
+ * Constants can be used as class properties, and need access to private
+ * and protected methods
+ */
+ friend class ConstantImpl;
};
/**
diff --git a/include/constant.h b/include/constant.h
index 619ce46..36b3417 100644
--- a/include/constant.h
+++ b/include/constant.h
@@ -47,6 +47,26 @@ public:
* Destructor
*/
virtual ~Constant() {}
+
+ /**
+ * Add the constant to a class
+ *
+ * You normally do not have to call this method yourself. You can simply
+ * do one of the following method calls to create class constants:
+ *
+ * myclass.property("MY_CONSTANT", "value", Php::Const);
+ * myclass.constant("MY_CONSTANT", "value");
+ * myclass.add(Php::Constant("MY_CONSTANT", "value"));
+ *
+ * All of the calls have the same result, it is up to you to decide which
+ * one suits you most. If you use the last one - using a Php::Constant
+ * class - the PHP-CPP library will call this "addTo()" method internally
+ * to forward the call to one of the other methods.
+ *
+ * @param clss Class to which the constant is added
+ * @internal
+ */
+ void addTo(ClassBase &clss);
private:
/**
diff --git a/zend/constant.cpp b/zend/constant.cpp
index 83a4de0..102e4f7 100644
--- a/zend/constant.cpp
+++ b/zend/constant.cpp
@@ -79,6 +79,32 @@ Constant::Constant(const char *name, const std::string &value) :
_impl(new ConstantImpl(name, value)) {}
/**
+ * Add the constant to a class
+ *
+ * You normally do not have to call this method yourself. You can simply
+ * do one of the following method calls to create class constants:
+ *
+ * myclass.property("MY_CONSTANT", "value", Php::Const);
+ * myclass.constant("MY_CONSTANT", "value");
+ * myclass.add(Php::Constant("MY_CONSTANT", "value"));
+ *
+ * All of the calls have the same result, it is up to you to decide which
+ * one suits you most. If you use the last one - using a Php::Constant
+ * class - the PHP-CPP library will call this "addTo()" method internally
+ * to forward the call to one of the other methods.
+ *
+ * @param clss Class to which the constant is added
+ * @internal
+ */
+void Constant::addTo(ClassBase &clss)
+{
+ // pass on to the implementation
+ _impl->addTo(clss);
+}
+
+
+
+/**
* End of namespace
*/
}
diff --git a/zend/constantimpl.h b/zend/constantimpl.h
index bf2ac25..bfae410 100644
--- a/zend/constantimpl.h
+++ b/zend/constantimpl.h
@@ -113,6 +113,52 @@ public:
virtual ~ConstantImpl() {}
/**
+ * Add the constant to a class
+ * @param clss The class to add it to
+ */
+ void addTo(ClassBase &clss)
+ {
+ // check the zval type
+ switch (Z_TYPE(_constant.value)) {
+
+ case IS_NULL:
+ // set a null constant
+ clss.property(_name, nullptr, Php::Const);
+ break;
+
+ case IS_LONG:
+ // set a long constant
+ clss.property(_name, Z_LVAL(_constant.value), Php::Const);
+ break;
+
+ case IS_DOUBLE:
+ // set a double constant
+ clss.property(_name, Z_DVAL(_constant.value), Php::Const);
+ break;
+
+ case IS_BOOL:
+ // set a boolean constant
+ clss.property(_name, Z_BVAL(_constant.value), Php::Const);
+ break;
+
+ case IS_STRING:
+ // set a string constant
+ clss.property(_name, std::string(Z_STRVAL(_constant.value), Z_STRLEN(_constant.value)), Php::Const);
+ break;
+
+ default:
+ // this should not happen, the constant can only be constructed as one
+ // of the above types, so it should be impossible to end up here. But
+ // for completeness, we convert the constant to a string, here
+ convert_to_string(&_constant.value);
+
+ // set as string constant
+ clss.property(_name, std::string(Z_STRVAL(_constant.value), Z_STRLEN(_constant.value)), Php::Const);
+ break;
+ }
+ }
+
+ /**
* Initialize the constant
* @param prefix Namespace prefix
* @param module_number The module number
diff --git a/zend/includes.h b/zend/includes.h
index e4dd6b9..874b0b1 100644
--- a/zend/includes.h
+++ b/zend/includes.h
@@ -77,8 +77,8 @@
#include "../include/classtype.h"
#include "../include/classbase.h"
#include "../include/interface.h"
-#include "../include/class.h"
#include "../include/constant.h"
+#include "../include/class.h"
#include "../include/namespace.h"
#include "../include/extension.h"
#include "../include/call.h"