How to: Building dynamic PHP objects and URL decoding example
PHP is a powerful programming language; with every release I’m amazed with the new power and functionality. The ability to build dynamic objects (the way I’ll describe) I believe was introduced as of PHP 5. In any case, this is good stuff. There are lots of reasons why you would want to create a dynamic object, in my example below; you’ll see how to create a dynamic object of the URL query parameters.
At the heart of building dynamic PHP objects are the PHP Magic Methods. From the PHP manual:
The function names __construct, __destruct (see Constructors and Destructors), __call, __get, __set, __isset, __unset (see Overloading), __sleep, __wakeup, __toString, __set_state, __clone and __autoload are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.
Magic methods get called automatically through the parser when certain events occur. For example, just before you transition pages all objects you’ve created will call their respective __sleep methods. Most likely you wouldn’t have this method defined, so nothing would happen.
So now lets look at what happens when a certain method (or variable) of a given PHP object is called. Take the following code as an example:
$x = new obj();
$x->method1();
$x->var1;
When a method of a given object is called (like method1) the first thing the parser does is look for the associated name in the instance of the object. If the method is found, then the parser gives control to the method. If the method is not found, then the __call function is called with the function name and input values as a parameter. If this function returns true, then the object will return successfully. If a __call function does not exist, the parser will check the parent class (if exists) and so forth.
Similarly, if a variable of an object is requested (like var1) the parser looks in the object for a variable with that name. If a variable does not exist, the __get method is called with the name of the variable as a parameter, and so forth.
You should be able to see how we can now simulate (fake) the existence of methods and variables by using these magic methods. The code below shows the parent class G_Dynamic. This class would be the parent to specific subclasses that would require dynamic method/variable access.
<?php
/* */
/**
*
* PHP versions 5.1.4
*
* George A. Papayiannis
*
* This class provides the magic functions needed to create
* a dynamic object. Subclasses would extend this object
* and call the constructor with a parsed array. See
* g_url_decode.class.php for an example of creating a
* dynamic object from the URL query string.
*
*/
/**
* Class definition
*/
class G_Dynamic {
private $param = array();
public function __construct($init) {
$this->param = $init;
}
private function __get($name) {
if (isset($this->param[$name])) {
$res = $this->param[$name];
} else {
$res = false;
}
return $res;
}
private function __set($name, $val) {
if (isset($this->param[$name])) {
$this->param[$name] = $val;
$res = true;
} else {
$res = false;
}
return $res;
}
private function __isset($name) {
return isset($this->param[$name]);
}
private function __unset($name) {
unset($this->param[$name]);
}
private function __call($name, $var) {
// add code to simulate function call
// return TRUE for success
}
}
?>
As an example, I created a subclass to G_Dynamic called G_URL_Decode. This class takes the URL query string as input, parses it into an array and calls the parents (G_Dynamic) constructor. The code for G_URL_Decode is below:
<?php
/* */
/**
*
* PHP versions 5.1.4
*
* George A. Papayiannis
*
* This class extends the G_Dynamic class to create a
* dynamic object of the URL query string. In another
* file, you would have:
*
* require_once "g_url_decode.class.php";
* $x = new G_URL_Decode($_SERVER['QUERY_STRING']);
*
* then you could have $x->(url param name) to access
*
*/
require_once "g_dynamic.class.php";
/**
* Class definition
*/
class G_URL_Decode extends G_Dynamic {
private $queryParam = array();
public function __construct($qs) {
parent::__construct($this->parseURL($qs));
}
public function getQueryParam() {
return $this->queryParam;
}
private function parseURL($qs) {
$this->queryParam = array();
$qs_parsed = explode("&", $qs);
foreach ($qs_parsed as $value) {
$paramVal = explode("=",$value);
if (array_key_exists(1,$paramVal)) {
$this->queryParam[htmlspecialchars(urldecode($paramVal[0]))] = htmlspecialchars(urldecode($paramVal[1]));
}
}
return $this->queryParam;
}
}
?>
Once the URL query string is successfully parsed, you can access the parameter names through the object. The code to bring it all together is below.
<?php
require_once "g_url_decode.class.php";
$x = new G_URL_Decode($_SERVER['QUERY_STRING']);
echo $x->var1;
?>
Using this as a base, you should be able to build some pretty cool dynamic objects.
Click here to download the source.


June 5th, 2008 at 3:35 am
If i change the name of the private function parseURL to parseURL1 then why does it not display error.
I have set error_reporting(E_ALL).
December 15th, 2008 at 1:11 am
Wow. This is the coolest script I’ve seen in a while.
December 22nd, 2008 at 12:20 am
Thanks Eric, Happy Holidays..
May 25th, 2009 at 4:35 am
Hi,
xml parser
I do need dynimically access object parameters from parser.But donot have any ideas on
how to dynamically access object parameters
Here is my static code:
foreach ($x->data->DIPENDRA20090320[0]->ENTRY as $valuei) {
echo “ID: “.$valuei->_text->ID;
echo “Name: “.$valuei->_text->NAME;
echo “Address: “$car->_text->ADDRESS;
}
Let me know whenever you read this message.I would really appreciate your help.
Regards,
Padma