为什么要返回新的静态? (PHP) [英] Why return new static? (PHP)

查看:78
本文介绍了为什么要返回新的静态? (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有些人创建一个返回新静态值的方法,而不是使所有方法都静态化?使用该方法返回新的静态变量的原因是什么? 我不是在问静态和自我之间有什么区别,或者什么是静态与静态?自卑.例如,这是一个简单的类:

Why some people create one method that returns new static instead of making all of the methods static? What is the reason to have that method that returns new static? I am not asking what is the difference between static and self, or what static & self mean. For example, here is one simple class:

<?php

class Expression
{
    public static function make()
    {
        return new static;
    }


    public function find($value)
    {
        return '/' . $value .'/';
    }

    public function then($value)  
    {
        return $this->find($value);
    }

    public function hi($value)  
    {
        return "hi";
    }

}

如您所见,有一个 static 方法 make(),该方法返回 new static .然后,有人调用了这样的其他方法:

As you can see, there is a static method make() which returns new static. Then, some people call other methods like this:

$regex = Expression::make()->find('www');

它的目的是什么?我看到这里我们不使用 new Expression 语法,如果那才是重点-那么为什么他们不将所有方法都设为静态方法呢?有什么区别,使用返回 new static 的方法的原因是什么?

What is the purpose of it? I see that here we don't use new Expression syntax, and if that's the point - then why they didn't make all methods to be, for example, static? What is the difference, what is the reason to have that method which returns new static?

推荐答案

new static实例化当前类中的一个新对象,并与后期静态绑定一起使用(如果该类是子类,则实例化该子类,我希望您理解).

new static instantiates a new object from the current class, and works with late static bindings (instantiates the subclass if the class was subclassed, I expect you understand that).

在类上具有static方法并返回相同的新实例的是替代构造函数.意思是,通常您的构造函数是public function __construct,通常它需要一堆参数:

Having a static method on a class which returns a new instance of same is an alternative constructor. Meaning, typically your constructor is public function __construct, and typically it requires a certain bunch of parameters:

class Foo {
    public function __construct(BarInterface $bar, array $baz = []) { ... }
}

具有替代构造函数允许您提供不同的默认值,或便捷的快捷方式来实例化此类,而不必提供那些特定的参数和/或能够提供替代构造函数将要提供的不同参数转换为规范的:

Having an alternative constructor allows you to provide different defaults, or convenience shortcuts to instantiate this class without having to supply those specific arguments and/or for being able to provide different arguments which the alternative constructor will convert to the canonical ones:

class Foo {

    public function __construct(BarInterface $bar, array $baz = []) { ... }

    public static function fromBarString($bar) {
        return new static(new Bar($bar));
    }

    public static function getDefault() {
        return new static(new Bar('baz'), [42]);
    }

}

现在,即使您的规范构造函数需要一堆复杂的参数,您也可以创建类的默认实例,只需使用Foo::getDefault(),它对于大多数用途可能就可以了.

Now, even though your canonical constructor requires a bunch of complex arguments, you can create a default instance of your class, which will probably be fine for most uses, simply with Foo::getDefault().

PHP中的典型示例是DateTime DateTime::createFromFormat .

The canonical example in PHP for this is DateTime and DateTime::createFromFormat.

在您的具体示例中,替代构造函数实际上不执行任何操作,因此它是多余的,但是我希望这是因为这是一个不完整的示例.如果确实有一个替代构造函数,除了new static之外什么也不做,那么它可能只是作为(new Foo)->上的便捷语法,我认为这是有问题的.

In your concrete example the alternative constructor doesn't actually do anything, so it's rather superfluous, but I expect that's because it's an incomplete example. If there's indeed an alternative constructor which does nothing other than new static, it's probably just meant as convenience syntax over (new Foo)->, which I find questionable.

这篇关于为什么要返回新的静态? (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆