简体   繁体   English

在PHP中链接一个新的对象实例

[英]Chaining a new object instance in PHP

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? 有没有办法在一行上进行相同的链接,而不创建虚拟函数?

PS: I want to skip the part where the constructor itself is on a new line. PS:我想跳过构造函数本身在新行上的部分。 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: 从PHP 5.4开始,已添加实例化的类成员访问权限,因此您可以这样做:

$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: 在以前的版本中,像你一样我讨厌你必须在一行上实例化对象然后开始在另一行上使用它,所以我有一个全局函数_i() ,如下所示:

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 :) 有些人会发现它很丑,但PHP缺乏语言表达能力,所以它对我有用:)

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 通常我有更多的静态方法来不同的用法

You can also use this technique from Singleton pattern (without using singleton pattern): 您也可以使用Singleton模式中的此技术(不使用单例模式):

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


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

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 您还可以使用PHP类型提示来确保仅将正确的对象用作参数

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

}

Sure is. 当然可以。 Simplt return this at the end of each function, to return the object so your next chained function can use it. Simplt return this每个函数结束时,返回对象让你的下一个链接的功能,可以使用它。

<?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
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM