Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 后期静态绑定_Php_Static_This - Fatal编程技术网

Php 后期静态绑定

Php 后期静态绑定,php,static,this,Php,Static,This,我们是否需要在以下条件下使用静态::$attribute而不是$this->attribute: b.php class B { public function tellAttribute(){ // $this OR static ?? echo $this->attribute; } } include 'b.php'; class A extends B { public $attribute = 'foo'; } $test = new A(); $test->

我们是否需要在以下条件下使用
静态::$attribute
而不是
$this->attribute

b.php

class B {
public function tellAttribute(){
// $this OR static ??
    echo $this->attribute;
}
}
include 'b.php';

class A extends B {
public $attribute = 'foo';
}

$test = new A();
$test->tellAttribute();
a.php

class B {
public function tellAttribute(){
// $this OR static ??
    echo $this->attribute;
}
}
include 'b.php';

class A extends B {
public $attribute = 'foo';
}

$test = new A();
$test->tellAttribute();

问这个问题是因为对我来说,除非我使用
static::$attribute
,否则它不应该起作用,但它仍然在回响foo。原因是什么?

是的,它会起作用<代码>$attribute是公共的。。。。。而且A还继承了
tellAttribute()
我不确定您在哪里期望得到什么。

您没有在任何地方使用“static”关键字。这里的常规类设置对我来说有点奇怪,但如果希望$attribute成为静态变量,则需要键入:

 public static $attribute = 'foo';

请记住,静态变量基本上是一个全局变量,您可以在此处查看更多内容:

类B定义了一个名为
tellAttribute()
的公共函数,如下所示:

public function tellAttribute(){
    echo $this->attribute;
}
然后实例化类A(类B的子类),并执行以下操作:

$test = new A();
$test->tellAttribute();
因此,实例化一个类为
A
的对象,然后在此对象上调用
tellAttribute()
。因为
tellAttribute()
方法使用了
$this
变量,所以您引用的是已实例化的实际对象。即使您在类
B
中定义了
tellAttribute()
-父对象-它实际上将指向具有公共
$attribute
属性的子对象(类
A
的实例)。这就是为什么它打印
foo
,以及为什么不需要使用
static::

另一方面,考虑这一点:

class B {
    public static $attribute = 'foo';
    public function tellAttribute(){
        echo self::$attribute; // prints 'foo'
    }
    public function tellStaticAttribute() {
        echo static::$attribute; // prints 'bar'
    }
}

class A extends B {
    public static $attribute = 'bar';
}

$test = new A();
$test->tellAttribute();
print "<BR>";
$test->tellStaticAttribute();
B类{
公共静态$attribute='foo';
公共函数属性(){
echo self::$attribute;//打印“foo”
}
公共函数属性(){
echo static::$attribute;//打印“条”
}
}
A类延伸至B类{
公共静态$attribute='bar';
}
$test=新的A();
$test->tellAttribute();
打印“
”; $test->tellStaticAttribute();
在本例中,我没有使用
$this
变量,而是使用
self:
static::
tellAttribute()
具有
self::
,并且将始终打印
foo
。这是因为
self::
只能引用当前类。
tellStaticAttribute()
使用
static::
并将“动态”打印类。我在技术术语等方面不是很在行,所以我会给你一个手册链接(我想你已经从你的帖子中读过了):

希望这能回答你的问题

我们是否需要使用static::$属性而不是$this->属性 在以下条件下:


,在您描述的场景中,您肯定不会使用
静态
关键字,并且没有理由不起作用。将
$this
的上下文看作是将所有不同的继承类“相加”为一个类的结果。也就是说,如果
类B扩展了A
,而
类C扩展了B
,那么通过实例化C,类A、B和C的所有属性和函数都可以通过类内的
$this
上下文获得,而C可以在其自身的函数中使用在B和反之亦然中定义的属性,因为所有东西都在那里,就好像它是实例中的一个独立类一样。

忘记静态,没有静态调用也没关系,因为这根本不是静态调用。在PHP中通常没有理由使用后期静态绑定,这只是让一些用户感到困惑的一个功能。@hakra
parent:
在处理从其他继承的类时实际上非常有用。我从Kevin Yank那里学到,我们需要使用
static::attribute
来访问继承的类的属性,
$this
不起作用。@Mahn:
parent:
不起作用static,与后期静态绑定无关。@YousufIqbal Kevin Yank(不管是谁)错了,除了shared关键字之外,与此无关。变量基本上从来都不是常量。不您可能是指一个全局变量。我问过您刚才回答的问题吗?我的意思是您没有进行后期静态绑定,因为您在上面的代码中处于非静态上下文中。如果你参考上面Pete171文章中的例子,你可以看到里面的例子。你只是在你发布的内容中做基本的继承,因此,没有理由$this不起作用。对我来说,如果我们在B类中使用
$this->attribute
,它将给出一个未定义属性的错误,因为php中早期的静态绑定。B类没有扩展任何内容,只是
A类扩展了B
,肯定会有错误……除非进一步定义为
private