ApnsPHP
ApnsPHP copied to clipboard
Allow chaining of the classes' functions
We should add a return this
to setters and most of the void functions.
This would allow chaining of the class functions. Consider this example:
I'm using this library and one of my classes stores an instance of the ApnsPHP_Message
class in a variable.
/**
* This is an example class that is only written here, and which isn't a real implementation
*/
class Example {
protected $_message;
public __construct(){
$this->_message = new ApnsPHP_Message();
}
public setMessage($message){
$this->_message = $message;
return $this;
}
public getMessage(){
return $this->_message;
}
// .... Let's consider we have all functionalities here ...
public send($recipients){
// Now if chaining was possible we could do this:
$this->getMessage()
->addRecipient($recipients[0])
->addRecipient($recipients[1])
->addRecipient($recipients[2])
->setText('This is an example')
->setBadge(1)
->setSound('text.mp3');
// Well you get the idea...
return $this;
}
}
Yeah danha, you are right! ;-)
But for what? You anyway will use something like it:
$message = new message();
for ( $i =0; $i<100; $i++ ) {
$message->addRecipient($recipients[$i])
}