diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/call.h | 15 | ||||
-rw-r--r-- | include/object.h | 20 | ||||
-rw-r--r-- | include/value.h | 45 |
3 files changed, 72 insertions, 8 deletions
diff --git a/include/call.h b/include/call.h index 16de1fe..1a0c59f 100644 --- a/include/call.h +++ b/include/call.h @@ -13,6 +13,21 @@ namespace Php { /** + * List of functions that are available for use in PHP + */ +extern bool class_exists(const char *classname, size_t size, bool autoload = true); +inline bool class_exists(const char *classname, bool autoload = true) { return class_exists(classname, strlen(classname), autoload); } +inline bool class_exists(const std::string &classname, bool autoload = true) { return class_exists(classname.c_str(), classname.size(), autoload); } +extern Value eval(const std::string &phpCode); +inline bool is_a(const Value &obj, const char *classname, size_t size, bool allow_string = false) { return obj.instanceOf(classname, size, allow_string); } +inline bool is_a(const Value &obj, const char *classname, bool allow_string = false) { return is_a(obj, classname, strlen(classname), allow_string); } +inline bool is_a(const Value &obj, const std::string &classname, bool allow_string = false) { return is_a(obj, classname.c_str(), classname.size(), allow_string); } +inline bool is_subclass_of(const Value &obj, const char *classname, size_t size, bool allow_string = true) { return obj.derivedFrom(classname, size, allow_string); } +inline bool is_subclass_of(const Value &obj, const char *classname, bool allow_string = true) { return is_subclass_of(obj, classname, strlen(classname), allow_string); } +inline bool is_subclass_of(const Value &obj, const std::string &classname, bool allow_string = true) { return is_subclass_of(obj, classname.c_str(), classname.size(), allow_string); } + + +/** * Call a function in PHP * @param name Name of the function to call * @param params Variable number of parameters diff --git a/include/object.h b/include/object.h index 2e6628a..6350259 100644 --- a/include/object.h +++ b/include/object.h @@ -41,11 +41,21 @@ public: */ Object(const Value &value) : Value() { - // string types are instantiated - if (value.isString()) instantiate(value); - - // otherwise copy the other object - else operator=(value); + // when a string is passed in, we are going to make a new instance of the + // passed in string + if (value.isString()) + { + // instantiate the object + instantiate(value); + + // and call the __construct method + call("__construct"); + } + else + { + // this simply copies the other object + operator=(value); + } } /** diff --git a/include/value.h b/include/value.h index f72304c..2fe940a 100644 --- a/include/value.h +++ b/include/value.h @@ -100,7 +100,7 @@ public: * @param value */ template <typename T> - Value(const std::map<std::string,T> &value) + Value(const std::map<std::string,T> &value) : Value(Type::Array) { // set all elements for (auto &iter : value) setRaw(iter.first.c_str(), iter.first.size(), iter.second); @@ -452,8 +452,6 @@ public: template <typename T> std::vector<T> vectorValue() const { - - // only works for arrays, other types return an empty vector if (!isArray()) return std::vector<T>(); @@ -502,6 +500,9 @@ public: // result variable std::map<std::string,T> result; + + // loop through the original map, and copy everything to the result + for (auto &iter : map) result[iter.first] = iter.second; // done return result; @@ -964,6 +965,37 @@ public: // try casting it return dynamic_cast<T*>(base); } + + /** + * Check whether this object is an instance of a certain class + * + * If you set the parameter 'allowString' to true, and the Value object + * holds a string, the string will be treated as class name. + * + * @param classname The class of which this should be an instance + * @param size Length of the classname string + * @param allowString Is it allowed for 'this' to be a string + * @return bool + */ + bool instanceOf(const char *classname, size_t size, bool allowString = false) const; + bool instanceOf(const char *classname, bool allowString = false) const { return instanceOf(classname, strlen(classname), allowString); } + bool instanceOf(const std::string &classname, bool allowString = false) const { return instanceOf(classname.c_str(), classname.size(), allowString); } + + /** + * Check whether this object is derived from a certain class. + * + * If you set the parameter 'allowString' to true, and the Value object + * holds a string, the string will be treated as class name. + * + * @param classname The class of which this should be an instance + * @param size Length of the classname string + * @param allowString Is it allowed for 'this' to be a string + * @return bool + */ + bool derivedFrom(const char *classname, size_t size, bool allowString = false) const; + bool derivedFrom(const char *classname, bool allowString = false) const { return derivedFrom(classname, strlen(classname), allowString); } + bool derivedFrom(const std::string &classname, bool allowString = false) const { return derivedFrom(classname.c_str(), classname.size(), allowString); } + private: /** @@ -1049,6 +1081,13 @@ protected: iterator createIterator(bool begin) const; /** + * Retrieve the class entry + * @param allowString Allow the 'this' object to be a string + * @return zend_class_entry + */ + struct _zend_class_entry *classEntry(bool allowString = true) const; + + /** * The Globals and Member classes can access the zval directly */ friend class Globals; |