开发者

Chaining a new object instance in PHP

开发者 https://www.devze.com 2023-03-29 12:16 出处:网络
We have the following chaining: $obj = new obj(); $obj->setname($params1)->setcolor($params2); Is there a way to do the same chaining on one line, without creating a dummy function?

We have the following chaining:

$obj = new obj();
$obj->setname($params1)->setcolor($params2);

Is there a way to do the same chaining on one line, without creating a dummy function?

P.S: I want to skip the 开发者_如何学JAVApart where the constructor itself is on a new line. I want to construct the object and start the chaining on the same line. Something like this:

$obj = new obj()->setname($params1)->setcolor($params2);


Since PHP 5.4, class member access on instantiation has been added so you can do it like this:

$obj = (new obj())->setname($params1)->setcolor($params2);

In previous versions, like you I hate that you have to instantiate the object on one line and then start using it on another, so I have a global function _i() which looks like this:

function _i($i) { return $i; }

I use it like this:

_i(new Obj)->doThis($param)->doThat($param2);

Some people will find it ugly but PHP lacks language expression power, so it works for me :)


I use static functions of class for it.


class a{
    static public function gets($args){
         return new self($args);
    }
    public function do_other(){

    }
}

a::gets()->do_other();

Usually there are more then I static method to different usages


Should be possible if you allways return the object itself in the function.

function setname($param) {
    // set the name etc..
    return $this;
}


You can also use PHP type hinting to make sure only the correct object is used as an argument

function instance(sdtClass $instance) { return $instance }

or as the static method using the class name

class CustomClass{
    static public function gets(CustomClass $obj){
        return $obj;
    }

}


You can also use this technique from Singleton pattern (without using singleton pattern):

<?php
class test
{
    public function __construct() {
    }
    public static function getInstance() {
        return new test();
    }
    public function chain() {
        echo 'ok';
    }
}


// outputs 'ok'
$test = test::getInstance()->chain();


Sure is. Simplt return this at the end of each function, to return the object so your next chained function can use it.

<?php
    class A
    {
        public __constructor()
        { .... }

        public function B($params)
        {
            //Processing
            return this;
        }

        public function C($params)
        {
            //Processing
            return this;
        }

        public function D($params)
        {
            //Processing
        }
    }
    $O = new A();
    $O->B($params)->C($params)->D($params); //Will work because B and C return this
    $O->B($params)->D($params)->C($params); //WILL NOT work because D doesn't return this
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消