method chaining in php5
Posted by mahmud ahsan on May 7, 2009
PHP
পিএইচপি৫ এ মেথড চেইনিং
when we design class we should keep in mind that, how we could make it more user friendly in usage purpose and development purpose. now i’m talking about a feature introduced by php5 called method chaining as part of the oop improvement over php4. this enable us to do pretty cool things like:
$obj->methodX()->methodY()->.............->methodZ();
a small change between php4 and php5 is, in php5 method can return object. but this change help us to think in a new way to handle object and it’s methods.
normally we use class in this way:
<?php
class Identity{
private $name;
private $occupation;
private $age;
function setName($name){
$this->name = $name;
}
function setAge($age){
$this->age = $age;
}
function setOccupation($occupation){
$this->occupation = $occupation;
}
function showIdentity(){
echo "Name: " . $this->name . "
";
echo "Age : " . $this->age . "
";
echo "Occupation: " . $this->occupation . "
";
}
}
$id = new Identity();
$id->setName("mahmud ahsan");
$id->setAge(25);
$id->setOccupation("software engineer");
$id->showIdentity();
?>
so we used 5 lines of code to use Identity object for our purpose. but if we use method chaining then instead of 5 lines, we can write 2 lines of code for this purpose. and it becomes more readable and easy to understand.
now look at the below code:
<?php
class Identity{
private $name;
private $occupation;
private $age;
function setName($name){
$this->name = $name;
return $this;
}
function setAge($age){
$this->age = $age;
return $this;
}
function setOccupation($occupation){
$this->occupation = $occupation;
return $this;
}
function showIdentity(){
echo "Name: " . $this->name . "
";
echo "Age : " . $this->age . "
";
echo "Occupation: " . $this->occupation . "
";
}
}
$id = new Identity();
$id->setName("mahmud ahsan")->setAge(25)->setOccupation("software engineer")->showIdentity();
?>
at the above code look, we use return $this in some method, that’s why we could easily call $id->setName(‘…’)->setAge(…) ;
I found this method chaining is using rapidly in the components of zend framework.
Random Posts
Comments (4)







hi,
nice to see your example, php started supporting method chaining since their php 5 release (perhaps?)
though those ugly “->” assignment signs are irresponsible for nifty code looking.
btw check out my one fluent interface example – http://we4tech.wordpress.com/2008/01/26/builder-pattern-with-fluent-interface-just-an-example-while-i-was-doing-by-routine-work/
don’t forget to read fluent interface http://www.martinfowler.com/bliki/FluentInterface.html
@hasan vai, thanks for your FluentInterface example’s links, i’m
checking it out…
True that -> operators are ugly. It could simply be a dot (.) like in most other popular languages. But method chaining saves lot of time and gives a birds eye view on a block of code. As i was writing JS codes (which had method chaining from the beginning) since 1998, i was missing the feature since i started using PHP (from version 3.5). It surely is one of the major things that PHP considered in version 5. Would be better if we see namespaces in PHP in future major releases.
Though i’ve heard about it and will figure it out after more study of php but since i’m in here i’ve just seen it.And now it is clear to me.Thanks bhaia.