ApnsPHP icon indicating copy to clipboard operation
ApnsPHP copied to clipboard

Allow chaining of the classes' functions

Open dannyhajj opened this issue 11 years ago • 2 comments

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;
    }
}

dannyhajj avatar Jun 18 '13 14:06 dannyhajj

Yeah danha, you are right! ;-)

duccio avatar Jul 25 '13 15:07 duccio

But for what? You anyway will use something like it:

$message = new message();
for ( $i =0; $i<100; $i++ ) {
    $message->addRecipient($recipients[$i])
}

ghost avatar Aug 01 '13 13:08 ghost