简体   繁体   English

这里有什么PHP课程?

[英]What is going on here in PHP with classes?

If I have this code, the string "test" is echoed. 如果我有这个代码,则回显字符串“test”。 This is in PHP 5.3. 这是在PHP 5.3中。 Is this some oversight that shouldn't be relied on, or is it some way of achieving multiple inheritence in PHP? 这是一些不应该依赖的疏忽,还是在PHP中实现多重继承的某种方式?

class Test1
{
    function getName()
    {
        return $this->name;
    }
}

class Test2
{
    public $name = 'test';

    function getName()
    {
        return Test1::getName();
    }
}

$test = new Test2;
echo $test->getName();

EDIT: 编辑:

As has been pointed out the comments by GZipp this is actually documented behaviour. 正如已经指出GZipp的评论,这实际上是记录在案的行为。 See this page: http://us2.php.net/manual/en/language.oop5.basic.php and the heading "Example #2 Some examples of the $this pseudo-variable". 参见本页: http//us2.php.net/manual/en/language.oop5.basic.php和标题“Example#2 $ this伪变量的一些例子”。

Classes A and B have a similar relationship to my two test classes above and the lines 类A和B与我上面的两个测试类和行有类似的关系

$b = new B();
$b->bar();

Show more or less the same result as my example. 显示与我的示例相同或多或少的结果。

Just to be clear - this isn't inheritance. 要明确一点 - 这不是继承。 Test2 does not extend Test1. Test2不扩展Test1。 You statically referenced a public method of the Test1 class. 您静态引用了Test1类的公共方法。

Nonetheless, the fact that it returns 'test' is interesting to say the least. 尽管如此,至少可以说它返回“测试”的事实很有意思。 And I see where it is giving off the idea of inheritance. 而且我看到它在哪里放弃了继承的想法。

If you can't find a decent answer, I'd submit your code as a bug. 如果您找不到合适的答案,我会将您的代码作为错误提交。

UPDATE UPDATE

It looks like under the hood, even though you statically referenced a method of Test1, it's still being called as Test2. 虽然你静态引用了Test1的一个方法,但它仍然被称为Test2。 In the end, this is undefined behavior, and ask noted above does throw a strict warning. 最后,这是未定义的行为,并且上面提到的问题确实会发出严格的警告。 Still very odd, and I personally agree that it shouldn't work. 仍然非常奇怪,我个人同意这不应该奏效。 But just to shed a little more insight which object it is using. 但只是为了更多地了解它所使用的对象。

class Test1 {
    function getName() {
        echo get_class($this);
        return $this->name;
    }
}
// ...
$test = new Test2;
echo $test->getName();

// echoes 'Test2 test'

PHP allows calling non-static methods as if they were static - that's a feature. PHP允许调用非静态方法,就像它们是静态的一样 - 这是一个特性。 PHP passes $this as an implicit parameter to such calls. PHP将$this作为隐式参数传递给此类调用。 Much like it does when calling a method the normal way. 就像以正常方式调用方法时一样。

But obviously PHP doesn't check whether the statically called class inherits the current one - and that's the bug. 但显然PHP不会检查静态调用的类是否继承当前类 - 这就是错误。

This is how you could think of what PHP does: 这就是你如何看待PHP的作用:

function Test1->getName($this = null) {
    return $this->name;
}

function Test2->getName($this = null) {
    return Test1->getName($this);
}

$test = new Test2;
echo $test->getName($test);

This behavior is wrong. 这种行为是错误的。 The correct Test2->getName would be: 正确的Test2->getName将是:

function Test2->getName($this = null) {
    return $this instanceof Test1 ? Test1->getName($this) : Test1->getName();
}

This has to be a bug. 这必须是一个错误。 100%. 100%。

  1. This is not inheritance. 这不是继承。
  2. You should not be able to call Test1::getName() as getName() in Test1 is not static 您应该无法调用Test1 :: getName(),因为Test1中的getName()不是静态的
  3. Even if you could call it, $this->name should not have access to the value in Test2. 即使您可以调用它,$ this-> name也不应该访问Test2中的值。

This breaks so many rules of OO I am seriously baffled how this got through testing. 这打破了OO的这么多规则我对这是如何通过测试感到困惑。 Well don for finding it!! 好吧,找不到它!

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

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