Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How do I chain methods in PHP?
The mutator methods can be used to chain methods, wherein these methods return the original objects, and other methods can be called on these objects that are returned by the mutator functions.
Example
Below is a simple example demonstrating the same −
<?php
class sample_class {
private $str;
function __construct() {
$this->str = "";
}
function addA() {
$this->str .= "am";
return $this;
}
function addB() {
$this->str .= "_bn";
return $this;
}
function getStr() {
return $this->str;
}
}
$new_object = new sample_class();
echo $new_object->addA()->addB()->getStr();
Output
This will produce the following output −
am_bn
Advertisements
