I don't know why I didn't think of this years ago but it is possible to method chain in php5. not just between subsidiary objects but within the same object.
in case it's useful for anyone consider this
the key bit is for your method to return an instance of its containing object.
the above code uses method overloading to create a method of setting properties. there are better ways to do this but the point of this code was to show how to chain methods rather than anything else.
in case it's useful for anyone consider this
Code:
<?php
class example{
public $name;
public $address;
public $telephone;
public $email;
public function __call($name, $value){
if (substr(strtolower($name), 0, 3) == 'set'){
$property = substr($name,3);
if ($this->isProperty($property)){
$this->$property = $value[0];
[red]return $this;[/red]
}
}
}
private function isProperty($property){
$vars = array_map('strtolower', array_keys(get_object_vars($this)));
return (in_array(strtolower($property), $vars));
}
public function output(){
echo "<pre>" . print_r($this, true);
}
}
$e = new example;
$e ->setname('Justin')
->setaddress('my address')
->settelephone('020 1234 5678')
->setemail('myemail@mydomain.com')
->output();
?>
the key bit is for your method to return an instance of its containing object.
the above code uses method overloading to create a method of setting properties. there are better ways to do this but the point of this code was to show how to chain methods rather than anything else.