- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
To Continue Learning Please Login
Login with Google