Nested Method calling in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

Nested Method calling in php

Post by tanvirtonu »

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..
User avatar
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

Post by AbraCadaver »

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

Post by tanvirtonu »

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->.............
User avatar
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

Post by AbraCadaver »

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.
User avatar
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

Post by AbraCadaver »

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

Post by tanvirtonu »

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();
NOW, THIS IS VERY CLEAR.....THANKS BRO..
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
Post Reply