This lesson shows how to use method chaining in PHP OOP.
With the help of php method chaining we can call more than one method or function of the class in single instruction.
$object->method_1()->method_2()The way to chain method invocations is to return the object from the previous call, using: return $this in the code of the previous method (method_1() ).
class Rectangle { public $x = 0; public $y = 0; // sets $x and $y values public function setXY($x, $y){ $this->x = $x; $this->y = $y; return $this; // returns object instance } // returns rectangle area public function area(){ return $this->x * $this->y; } // returns rectangle perimeter public function perimeter(){ return 2 * ($this->x + $this->y); } } // creates an object instance of the class $obR = new Rectangle; // outputs area and perimeter echo $obR->setXY(3, 4)->area(); // 12 echo '<br/>'. $obR->setXY(3, 4)->perimeter(); // 14
class setTag { private $id = ''; // tag id attribute private $class = ''; // tag class attribute // sets $id public function setId($id){ $this->id = ' id="'. $id .'"'; return $this; // returns object instance } // sets $class public function setClass($class){ $this->class = ' class="'. $class .'"'; return $this; // returns object instance } // returns the HTML tag and its content public function getTagCnt($tag, $cnt){ return '<'. $tag .$this->id. $this->class .'>'. $cnt .'</'. $tag. '>'; } } // creates an object of the class $obTag = new setTag; // variables with tag type and content $tag = 'div'; $cnt = 'https://coursesweb.net'; // chains methods to set ID, Class, and output HTML <div> tag with these attributes and content in a single instruction echo $obTag->setId('some_id')->setClass('cls')->getTagCnt($tag, $cnt); /* Outputs: <div id="some_id" class="cls">https://coursesweb.net</div> */The instruction that chains the methods in the code above:
// sets ID and css class, then outputs the HTML tag with content $obTag->setId('some_id'); $obTag->setClass('cls'); echo $obTag->getTagCnt($tag, $cnt);
className::staticMethod()->otherMethod()- Example, a PHP class with a static method that sets the value of a private static property (private cannot be accessed outside class), and a public method that returns the private property value.
<?php class clsData { // private property to cannot be accessed outside class private static $data; // static method that sets $data value public static function setData(){ self::$data = 'https://coursesweb.net/php-mysql/'; return new self; // returns class instance } // method to return $data value public function getData(){ return self::$data; } } // accessing getData() method after setData() echo clsData::setData()->getData(); // https://coursesweb.net/php-mysql/
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net