PHPUnit-使用$ this或self作为静态方法吗? [英] PHPUnit - Use $this or self for static methods?

查看:82
本文介绍了PHPUnit-使用$ this或self作为静态方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想写很长的文字,因为这是一个简短的问题. PHPUnit测试包含几种静态方法.例如,所有这些 \PHPUnit\Framework\Assert::assert*()方法,以及identicalToequalTo.

I don't want to write a long text, because it is a short question. PHPUnit tests contain several methods that are static. For example all those \PHPUnit\Framework\Assert::assert*() methods and also the identicalTo, equalTo.

我的IDE(具有IntelliSense/自动补全功能)不接受$this的呼叫,但接受self的呼叫.我了解到应该通过类而不是对象来调用静态函数,因此self.

My IDE (with IntelliSense/autocompletion) doesn't accept calls with $this, but with self. I have learned that static functions should be called through the class, not an object, so self.

更正确的是什么?

$this->assertTrue('test');

self::assertTrue('test');

?

(如果"$ this"更正确,您能否指出为什么我们不应该使用"self"?)

(And if "$this" is more correct, can you maybe point out why we should not use "self"?)

推荐答案

通常,self仅用于引用静态方法和属性(尽管您会困惑地可以引用非静态方法) (self)和静态方法($this),前提是self调用的方法不引用$this.)

Generally, self is only used to refer to static methods and properties (though confusingly you can refer to non-static methods with self, and to static methods with $this, provided the methods called with self don't reference $this.)

<?php
class Test {
    public static function staticFunc() {echo "static ";}
    public function nonStaticFunc() {echo "non-static\n";}
    public function selfCaller() {self::staticFunc(); self::nonStaticFunc();}
    public function thisCaller() {$this->staticFunc(); $this->nonStaticFunc();}
}
$t = new Test;
$t->selfCaller();  // returns "static non-static"
$t->thisCaller();  // also returns "static non-static"

在处理$thisself时,记住继承非常重要. $this将始终引用当前对象,而self则引用使用self的类.现代PHP还通过static关键字,其作用与静态功能$this相同(并且应优先于$this来使用.)

Inheritance is important to remember when dealing with $this or self. $this will always refer to the current object, while self refers to the class in which self was used. Modern PHP also includes late static binding via the static keyword, which will operates the same way as (and should be preferred over) $this for static functions.

<?php
class Person {
    public static function whatAmI() {return "Human";}    
    public function saySelf() {printf("I am %s\n", self::whatAmI());}
    public function sayThis() {printf("I am %s\n", $this->whatAmI());}
    public function sayStatic() {printf("I am %s\n", static::whatAmI());}
}

class Male extends Person {
    public static function whatAmI() {return "Male";}
}

$p = new Male;
$p->saySelf();    // returns "I am Human"
$p->sayThis();    // returns "I am Male"
$p->sayStatic();  // returns "I am Male"

特别是关于PHPUnit,看来他们只是按照自己一贯的方式他们!尽管根据他们的文档,

As regards PHPUnit in particular, it appears they simply do things the way they've always done them! Though according to their documentation, your code should work fine using static methods.

这篇关于PHPUnit-使用$ this或self作为静态方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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