*sigh* Long time since I’ve posted… Interesting times with programming languages. Anywho, I’ve been wondering how I could push responsibilities onto objects and further define an object without defining them inside of the class from the start.
So I thought, hey let me create a function that’ll create an array that I could turn into an object like this:
function defineObject($data, $methods){
$resultObj = array();
// Merge data into object as properties
// Merge methods into object as methods…. $methods are an array of anonymous functions
return (object)$resultObj;
}
and suree that’ll probably work, but that’s not what I want. Maybe I could turn an already created object into an array and extend its properties and methods from there, but my issue is that I might want this specific object on creation to have these properties and methods without me calling another function to do it… “Haha lazy” you say, but I think its me wanting to make my life a little easier. I think I’ve come up with a more elegant way of extending objects and I think you are going to love how it works.
Essentially, we are going to use php’s special object setter, getter, call functions to emulate what I want to do, but we’ll have to create two other protected function for creating dynamically creating values for the object. I call it ExClass, for Extensible Class. Call it whatever you’d like.
class ExClass
{
protected $_data;
public function __construct()
{
$this->_data = array();
}
public function __get($key)
{
// First, check if the object’s property exists and return that
// Elseif, check if the property exists in the data array and return that
// Else return null/// possibly log the error
}
public function __set($key, $value)
{
// First, check if the object’s property exists
// Elseif, check if the property exists in the data array
// Else return null/// possibly log the error
}
protected function __call($method, $arguments)
{
// The beauty of this function is that only undefined methods are sent to it, so we don’t have to see if the method exists in the object. Also $arguments is an array of arguments sent to the object.
// First, we ask the object for the method in its data array, by the usual
$Objmethod = $this->method;
// Then we’ll call the function and you can do with in whatever way you’d like
return $objMethod($this->_data, $arguments);
// OR
// Prepend the data into the arguments.. use array_unshift(NULL) and then set $arguments[0] =&$this->_data;
return call_user_func_array($objMethod, $arguments);
// The reasoning for the object data array being included is so we can use and potentially modify the object data inside of the function. I wrote this function without using the use keyword because I’ve never used it before, but it looks easy to use. As you’ll notice object properties are kept away from these functions as I treat object properties as something we want completely hidden away, though it really depends on how you'll want to implement it.
}
// These two functions below do not have to but protected, though for my purposes they are.
protected function _cv($key, $value)
{
// Store the key –> value into the _data array
}
protected function _sv($key, $value)
{
// check if the key exists in the _data array, else return
// If key exists, then set it
// It is possible this is a redundant function, but heck we’ll include it
}
}
I prefer not to fill in every line as a lot of it is easily done. Anyway, I would like to mention, that there may be some errors in this post, but that’s completely fine. Just mention it and it’ll be fixed.
Any class that extends this class, will have to call the parent::__construct() to initialize the data class and anything you put into the parent construct. This is not a requirement and completely optional, but you will have to initialize the data array, if you choose not to use the parent constructor.
I trust you understand the concept. This object can now get/set properties that are stored in the data array using $obj->blahKey; and you can call unofficially defined functions such as: $obj->blahFunc($arg1, $arg2);, but we are not done yet. This is the basis for creating object modules.
Object Modules as I call them are just a way to create a bunch of other classes called modules with a static function called initialize(), but I can describe it later when I have time. I hope this was an informative read.