Chaining Static Method

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() ).

- Here is an example, a PHP class that can be used to calculate the area and perimeter of rectangle (see the comments in code, and test it yourself).
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

• You can chain more than two methods, the technique is the same, all the previous methods must return the object instance.
- Here is an example that chains three methods. A PHP class to define HTML tag with ID, CSS class, and content (study the code and test it yourself).
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 = 'http://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">http://coursesweb.net</div>
*/
The instruction that chains the methods in the code above:
echo $obTag->setId('some_id')->setClass('cls')->getTagCnt($tag, $cnt);
It is the same with this:
// sets ID and css class, then outputs the HTML tag with content
$obTag->setId('some_id');
$obTag->setClass('cls');
echo $obTag->getTagCnt($tag, $cnt);

Chaining Static Method

To can chain a static method you must return the Instance of the class in static method, using: return new self.
In the instruction that calls and chains the static method use the Class Name.
Syntax:
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 = 'http://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();       // http://coursesweb.net/php-mysql/

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
PHP Method Chaining

Last accessed pages

  1. Drag Racer V3 (14584)
  2. The Mastery of Love (1687)
  3. Batting Champ (657)
  4. Bubble Tanks 3 (24679)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (35549)

Popular pages this month

  1. Qwop (6859)
  2. Drag Racer V3 (4689)
  3. Bubbles3 (1916)
  4. List with All the Games (1605)
  5. Butterfly Kyodai (1507)