在PHP5中,使用self和$this有什么区别?
什么时候合适?
在PHP5中,使用self和$this有什么区别?
什么时候合适?
当前回答
在PHP中,使用self关键字访问静态财产和方法。
问题是,无论method()是否声明为静态,都可以在任何地方用self::method(。那么你应该使用哪一种?
考虑以下代码:
class ParentClass {
function test() {
self::who(); // will output 'parent'
$this->who(); // will output 'child'
}
function who() {
echo 'parent';
}
}
class ChildClass extends ParentClass {
function who() {
echo 'child';
}
}
$obj = new ChildClass();
$obj->test();
在本例中,self::who()将始终输出“parent”,而$this->who(()将取决于对象的类。
现在我们可以看到self是指调用它的类,而$this是指当前对象的类。
因此,只有当$this不可用时,或者当您不想让后代类覆盖当前方法时,才应该使用self。
其他回答
以下是$this和self用于非静态的正确用法示例和静态成员变量:
<?php
class X {
private $non_static_member = 1;
private static $static_member = 2;
function __construct() {
echo $this->non_static_member . ' '
. self::$static_member;
}
}
new X();
?>
self::用于当前类的关键字,基本上用于访问静态成员、方法和常量。但是在$this的情况下,不能调用静态成员、方法和函数。
您可以在另一个类中使用self::关键字并访问静态成员、方法和常量。当它将从父类扩展时,在$this关键字的情况下也是如此。当另一个类将从父类扩展时,您可以访问该类中的非静态成员、方法和函数。
下面给出的代码是self::和$this关键字的示例。只需复制并粘贴代码文件中的代码,即可看到输出。
class cars{
var $doors = 4;
static $car_wheel = 4;
public function car_features(){
echo $this->doors . " Doors <br>";
echo self::$car_wheel . " Wheels <br>";
}
}
class spec extends cars{
function car_spec(){
print(self::$car_wheel . " Doors <br>");
print($this->doors . " Wheels <br>");
}
}
/********Parent class output*********/
$car = new cars;
print_r($car->car_features());
echo "------------------------<br>";
/********Extend class from another class output**********/
$car_spec_show = new spec;
print($car_spec_show->car_spec());
此外,由于$this::尚未讨论。
仅供参考,从PHP 5.3开始,当处理实例化对象以获取当前作用域值时,与使用static::不同,可以选择使用$this::这样。
http://ideone.com/7etRHy
class Foo
{
const NAME = 'Foo';
//Always Foo::NAME (Foo) due to self
protected static $staticName = self::NAME;
public function __construct()
{
echo $this::NAME;
}
public function getStaticName()
{
echo $this::$staticName;
}
}
class Bar extends Foo
{
const NAME = 'FooBar';
/**
* override getStaticName to output Bar::NAME
*/
public function getStaticName()
{
$this::$staticName = $this::NAME;
parent::getStaticName();
}
}
$foo = new Foo; //outputs Foo
$bar = new Bar; //outputs FooBar
$foo->getStaticName(); //outputs Foo
$bar->getStaticName(); //outputs FooBar
$foo->getStaticName(); //outputs FooBar
使用上面的代码不是常见的或推荐的做法,只是为了说明它的用法,在引用原海报的问题时更像是“你知道吗?”。
它还表示$object::CONSTANT的用法,例如echo$foo::NAME;与$this::NAME相反;
情况1:使用self可以用于类常量
class classA { const FIXED_NUMBER = 4; self::POUNDS_TO_KILOGRAMS }
如果要在类外部调用它,请使用classA::POUNDS_to_KILOGAMS访问常量
情况2:对于静态财产
class classC { public function __construct() { self::$_counter++; $this->num = self::$_counter; } }
这里有一个小的基准(第7.2.24节):
Speed (in seconds) Percentage
$this-> 0.91760206222534 100
self:: 1.0047659873962 109.49909865716
static:: 0.98066782951355 106.87288857386
4000 000次运行的结果。结论:没关系。这是我使用的代码:
<?php
class Foo
{
public function calling_this() { $this->called(); }
public function calling_self() { self::called(); }
public function calling_static() { static::called(); }
public static function called() {}
}
$foo = new Foo();
$n = 4000000;
$times = [];
// warmup
for ($i = 0; $i < $n; $i++) { $foo->calling_this(); }
for ($i = 0; $i < $n; $i++) { $foo->calling_self(); }
for ($i = 0; $i < $n; $i++) { $foo->calling_static(); }
$start = microtime(true);
for ($i = 0; $i < $n; $i++) { $foo->calling_this(); }
$times["this"] = microtime(true)-$start;
$start = microtime(true);
for ($i = 0; $i < $n; $i++) { $foo->calling_self(); }
$times["self"] = microtime(true)-$start;
$start = microtime(true);
for ($i = 0; $i < $n; $i++) { $foo->calling_static(); }
$times["static"] = microtime(true)-$start;
$min = min($times);
echo $times["this"] . "\t" . ($times["this"] / $min)*100 . "\n";
echo $times["self"] . "\t" . ($times["self"] / $min)*100 . "\n";
echo $times["static"] . "\t" . ($times["static"] / $min)*100 . "\n";