I m new to php programming. I want to know what is the meaning of double " -> " in php method calling. Like the one in the following example -
$this->load->library('class_name', $config, 'object name')
Does it mean that the "load" method has the "library" method in it...I m confused..
Nested Method calling in php
Moderator: General Moderators
-
tanvirtonu
- Forum Commoner
- Posts: 35
- Joined: Wed Oct 17, 2007 9:15 am
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Nested Method calling in php
It is called method chaining. So long as the method returns an object you can do this. load() is called and it returns an object, most likely $this and then the library() method is called. Pretty much the same as this:
Code: Select all
$this->load();
$this->library('class_name', $config, 'object name');mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
tanvirtonu
- Forum Commoner
- Posts: 35
- Joined: Wed Oct 17, 2007 9:15 am
Re: Nested Method calling in php
THANKS A LOT BRO...
But I think load doesnt return $this object, I guess, $this calla load, then load return some object which in turn call library method, correct me if I am wrong
Can I do this then (if possible) - $obj1->$obj2->$obj3->$obj4->$obj5->.............
But I think load doesnt return $this object, I guess, $this calla load, then load return some object which in turn call library method, correct me if I am wrong
Can I do this then (if possible) - $obj1->$obj2->$obj3->$obj4->$obj5->.............
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Nested Method calling in php
Well no. The -> is accessing a property or method. It works like this:
Code: Select all
class Test {
function load() {
// do some stuff
return $this;
}
function execute() {
// do more stuff
return $this;
}
function display() {
// display something
}
}
$obj = new Test();
$obj->load()->execute()->display();mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Nested Method calling in php
For the original code to work, load would be an object property of $this that is an object itself that contained a method called library().tanvirtonu wrote:THANKS A LOT BRO...
But I think load doesnt return $this object, I guess, $this calla load, then load return some object which in turn call library method, correct me if I am wrong
Can I do this then (if possible) - $obj1->$obj2->$obj3->$obj4->$obj5->.............
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
tanvirtonu
- Forum Commoner
- Posts: 35
- Joined: Wed Oct 17, 2007 9:15 am
Re: Nested Method calling in php
NOW, THIS IS VERY CLEAR.....THANKS BRO..AbraCadaver wrote:Well no. The -> is accessing a property or method. It works like this:Code: Select all
class Test { function load() { // do some stuff return $this; } function execute() { // do more stuff return $this; } function display() { // display something } } $obj = new Test(); $obj->load()->execute()->display();
But cant it be different object in each method call , e.g. - $obj1 -> method1 - > method2 , here method1 returns $obj2 which has the method2 , cant it work this way