Daisy Chaining Class Methods / Hindering Performance?

Say I have an object called Message. Inside Message are methods such as:


public function setSender()
{
   # Some functionality
   return $this;
}

public function setMessage()
{
   # Some functionality
   return $this;
}

public function etc.....

Each of these methods return $this so that I can call several methods from a single command, like:


$this->message
	->setSender($this->userID())
	->setRecipient($this->recipient)
	->setSubject($this->db->sanitizeData($_POST['subject']))
	->$this->db->sanitizeData($_POST['message'])
	->save();

But does passing the entire object back and forth hinder performance? Depending on how much data and/or other objects held within $this, I’d imagine this technique might be coder friendly, but could hurt processing time.

Hi brandonBuster,

I don’t believe passing the object back in method chaining back requires any more processing time then instantiating and then calling each method separately. Method chaining is complied into PHP so, chances that it take any more or less time then parsing the sequential code could be debated. But IMHO the code reads better using chaining than the more traditional PHP class and method ways.

Steve

$this contains a reference to the object it doesn’t contain the data itself. Think of it as a pointer. Thus it won’t matter how big your object is passing a reference incurs no penalty.

Yeah, I don’t think there really is any difference in performance. Even if there were, it would be negligible and it would be outweighed by the benefits that come with having cleaner, more readable code.

This is true for PHP 5 to an extent, it may be by reference or an identifier, either way, it isn’t the actual object itself, it is a pointer to the object. Thus in my opinion, your daisy chaining is acceptable by all means of performance. The only time it would not be, is if you were forcing the object itself to be returned instead of the identifier or the reference pointer which isn’t default behavior.

One of the key-points of PHP 5 OOP that is often mentioned is that “objects are passed by references by default”. This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn’t contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Source

If you are using anything below PHP 5.3, you are in unsupported waters. Everything below 5.3 is no longer supported, no security updates either. So yeah, update to 5.3 or later.

:smiley: I hope not many people are using a version of php below 5.3, but it’s hard to say. Thankfully if you are using 5 or higher you are okay with how objects are passed around in PHP except for oddball cases as described in the Source link I provided. :slight_smile:

Thanks for the replies. They were all informative.