2 Replies - 447 Views - Last Post: 07 April 2010 - 08:30 AM Rate Topic: -----

#1 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

php class function chaining

Posted 07 April 2010 - 07:24 AM

Is there a way to chain functions of a class together? Similar to how jQuery allows you to chain function calls together.

Ex.

<?php
class foobar
{
	function foo()
	{
	}
		
	function bar()
	{
	}
}

$fb = new foobar();

$fb->foo()->bar();
?>


Is This A Good Question/Topic? 0
  • +

Replies To: php class function chaining

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: php class function chaining

Posted 07 April 2010 - 08:19 AM

Yes. You just need your methods to return the appropriate object. To keep operating on the same object, you'd just return $this. For instance:
<?php
class foobar {
	function foo() {
		# Do some stuff
		return $this;
	}
		
	function bar() {
		# Do some other stuff
		return $this;
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

Re: php class function chaining

Posted 07 April 2010 - 08:30 AM

View PostAdaHacker, on 07 April 2010 - 07:19 AM, said:

Yes. You just need your methods to return the appropriate object. To keep operating on the same object, you'd just return $this. For instance:
<?php
class foobar {
	function foo() {
		# Do some stuff
		return $this;
	}
		
	function bar() {
		# Do some other stuff
		return $this;
	}
}



Cool, thank you. I didn't know you could use $this for that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1