是否有可能过度使用PHP中的后期静态绑定? [英] Is it possible to overuse late static binding in PHP?

查看:84
本文介绍了是否有可能过度使用PHP中的后期静态绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从5.3版开始,PHP支持后期绑定用于静态方法.虽然这无疑是有用的功能,但只有极少数情况下确实需要使用它(例如,Active Record模式).

Starting with version 5.3, PHP supports late binding for static methods. While it's an undoubtedly useful feature, there are only several cases where its use is really necessary (e.g. the Active Record pattern).

考虑以下示例:

1.便捷构造函数(::create())

1. Convenience constructors (::create())

class SimpleObject
{
    public function __construct() { /* ... */ }

    public static function create()
    {
        return new static; // or: return new self;
    }
}

如果可以扩展此类(但是,同一包中的任何类都不能扩展该类),则应使用后期静态绑定只是为了使其扩展更容易(而不必重写::create()方法,并且,更多重要的是,不必记住要这样做)?

If this class may be extended (however, it's not extended by any class in the same package), should late static binding be used just to make extending it easier (without having to rewrite the ::create() method, and, more importantly, without having to remember to do that)?

注意:此惯用法用于解决无法在刚刚构造的对象上调用方法的问题:new SimpleObject()->doStuff()在PHP中无效.

Note: this idiom is used to work around the impossibility to call methods on just constructed objects: new SimpleObject()->doStuff() is invalid in PHP.

2.类常量

class TagMatcher
{
    const TAG_PATTERN = '/\<([a-z\-]+?)\>/i';

    private $subject;

    public function construct($subject) { $this->subject = $subject; }

    public function getAllTags()
    {
        $pattern = static::TAG_PATTERN;
        preg_match_all($pattern, $this->subject);
        return $pattern[1];
    }
}

在此示例中使用static::的原因与上一个类似.之所以使用它,是因为可以通过扩展该类并覆盖常量来使此类匹配不同格式的标签.

The reason to use static:: in this example is similar to the previous one. It's used just because this class can be made to match differently formed tags just by extending it and overriding the constant.

总而言之,后期静态绑定的这些用途(和类似用途)是否过大?是否有明显的性能下降?另外,频繁使用后期绑定是否会降低操作码缓存所带来的整体性能提升?

So, to wrap it all up, are these uses (and similar ones) of late static binding are an overkill? Is there any noticeable performance hit? Also, does frequent use of late binding reduce the overall performance boost given by opcode caches?

推荐答案

总而言之,后期静态绑定的这些用途(和类似用途)是否过大?是否有明显的性能下降?另外,频繁使用后期绑定是否会降低操作码缓存所带来的整体性能提升?

So, to wrap it all up, are these uses (and similar ones) of late static binding are an overkill? Is there any noticeable performance hit? Also, does frequent use of late binding reduce the overall performance boost given by opcode caches?

后期静态绑定的引入修复了PHP对象模型中的一个缺陷.这与性能无关,而与语义有关.

The introduction of late static binding fixes a flaw in PHP's object model. It's not about performance, it's about semantics.

例如,只要方法的实现不使用$this,我就喜欢使用静态方法.仅仅因为方法是静态的,并不意味着您有时不想覆盖它.在PHP 5.3之前的行为是,如果您覆盖静态方法,则不会标记任何错误,但是PHP会继续前进,并默默使用父级版本.例如,下面的代码在PHP 5.3之前显示"A".这是非常出乎意料的行为.

For example, I like to use static methods whenever the implementation of the method doesn't use $this. Just because a method is static doesn't mean to say that you don't want to override it sometimes. Prior to PHP 5.3, the behavior was that no error was flagged if you overrode a static method, but PHP would just go ahead and silently use the parent's version. For example, the code below prints 'A' before PHP 5.3. That's highly unexpected behavior.

最新的静态绑定对其进行了修复,现在相同的代码显示为"B".

Late static binding fixes it, and now the same code prints 'B'.

<?php
class A {
  public static function who() {
    echo __CLASS__;
  }
  public static function test() {
    static::who();
  }
}

class B extends A {
  public static function who() {
    echo __CLASS__;
  }
}

B::test();
?>

这篇关于是否有可能过度使用PHP中的后期静态绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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