PHP OOP:方法链接

发布于 2024-09-04 08:45:30 字数 525 浏览 7 评论 0原文

我有以下代码,

<?php
class Templater
{
    static $params = array();

    public static function assign($name, $value)
    {
        self::$params[] = array($name => $value);
    }

    public static function draw()
    {
        self::$params;
    }
}


 $test = Templater::assign('key', 'value');
 $test = Templater::draw();
 print_r($test);

如何更改此脚本以便我可以使用它?

$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);

I have the following code,

<?php
class Templater
{
    static $params = array();

    public static function assign($name, $value)
    {
        self::$params[] = array($name => $value);
    }

    public static function draw()
    {
        self::$params;
    }
}


 $test = Templater::assign('key', 'value');
 $test = Templater::draw();
 print_r($test);

How can I alter this script so I could use this?

$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝海 2024-09-11 08:45:30

您不能将方法链接与静态方法一起使用,因为您无法返回类级别范围(return self 不行)。将您的方法更改为常规方法,并在您想要允许链接的每个方法中返回 $this

请注意,您不应使用 T_PAAMAYIM_NEKUDOTAYIM 访问实例方法,因为它会引发 E_STRICT 通知。使用T_OBJECT_OPERATOR来调用实例方法。

另请参阅:

You cannot use Method Chaining with static methods because you cannot return a class level scope (return self won't do). Change your methods to regular methods and return $this in each method you want to allow chaining from.

Notice that you should not use T_PAAMAYIM_NEKUDOTAYIM to access instance methods as it will raise an E_STRICT Notice. Use T_OBJECT_OPERATOR for calling instance methods.

Also see:

独留℉清风醉 2024-09-11 08:45:30

您不应该使用静态成员:

class Templater
{
    private array $params = [];

    public function assign($name, $value) : self
    {
        $this->params[$name] = $value;

        return $this;
    }

    public function draw()
    {
        // do something with $this->params
    }
}

$test = (new Templater())->assign('key', 'value')->assign('key2', 'value2')->draw();

You shouldn't be using static members:

class Templater
{
    private array $params = [];

    public function assign($name, $value) : self
    {
        $this->params[$name] = $value;

        return $this;
    }

    public function draw()
    {
        // do something with $this->params
    }
}

$test = (new Templater())->assign('key', 'value')->assign('key2', 'value2')->draw();
幼儿园老大 2024-09-11 08:45:30

只需使用实例变量和实例函数而不是静态变量和实例函数。

<?php
class Templater
{
    $params = array();

    public function assign($name, $value)
    {
        $this->params[] = array($name => $value); 
        return $this;
    }

    public function draw()
    {
        echo $this->params;
        return $this;
    }
}

$test = new Templater();
$test->assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);

Just use instance variables and instance functions instead of static ones.

<?php
class Templater
{
    $params = array();

    public function assign($name, $value)
    {
        $this->params[] = array($name => $value); 
        return $this;
    }

    public function draw()
    {
        echo $this->params;
        return $this;
    }
}

$test = new Templater();
$test->assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);
千柳 2024-09-11 08:45:30

////////

类模板器
{
静态$params = array();

public static function assign($name, $value)
{
    self::$params[] = array($name => $value);
    return new Templater;
}

public static function draw()
{
    return self::$params;
}

$

test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($测试);

////////

class Templater
{
static $params = array();

public static function assign($name, $value)
{
    self::$params[] = array($name => $value);
    return new Templater;
}

public static function draw()
{
    return self::$params;
}

}

$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);

楠木可依 2024-09-11 08:45:30

通常,将静态调用和实例调用混合在一起是一种糟糕的形式,忽略该调用(除非您给出一个需要静态的理由)。您正在使用的另一个概念是调用链,它是使用返回来实现的。

class Templater
{
    protected $params = array();

    public function assign($name, $value) {
        $this->params[] = array($name => $value);
        return $this;
    }

    public function draw() {
        // do drawing w/ $this->params;
        return $this;
    }
}

Mixing a static and an instance call like that is poor form in general, omitting that one (unless you give a reason that it needs to be static). The other concept you're working with is call chaining, which is implemented using returns.

class Templater
{
    protected $params = array();

    public function assign($name, $value) {
        $this->params[] = array($name => $value);
        return $this;
    }

    public function draw() {
        // do drawing w/ $this->params;
        return $this;
    }
}
总以为 2024-09-11 08:45:30
class Templater
{   
    public static $params;

    private static $_instance = null;

    public static function init()
    {
        if (self::$_instance === null)
        {
            self::$_instance = new self;
        }

        return self::$_instance;
    }

    public function assign($name, $value)
    {
        self::$params[$name] = $value;
        return $this;
    }

    public function draw()
    {
        return self::$params;
    }
}

$test = Templater::init()->assign('key', 'value')->assign('key2', 'value2')->draw();
class Templater
{   
    public static $params;

    private static $_instance = null;

    public static function init()
    {
        if (self::$_instance === null)
        {
            self::$_instance = new self;
        }

        return self::$_instance;
    }

    public function assign($name, $value)
    {
        self::$params[$name] = $value;
        return $this;
    }

    public function draw()
    {
        return self::$params;
    }
}

$test = Templater::init()->assign('key', 'value')->assign('key2', 'value2')->draw();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文