PHP类:什么时候使用:: vs. - > ;? [英] PHP Classes: when to use :: vs. ->?
本文介绍了PHP类:什么时候使用:: vs. - > ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道有两种方法可以访问PHP类 - ::和 - >。有时一个似乎为我工作,而另一个没有,我不明白为什么。
I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.
每个的好处是什么,什么是权利
What are the benefits of each, and what is the right situation to use either?
推荐答案
简单地说, ::
- >
用于对象级属性。
Simply put, ::
is for class-level properties, and ->
is for object-level properties.
如果属性属于类,请使用 ::
If the property belongs to the class, use ::
属于 类的实例,请使用 - >
If the property belongs to an instance of the class, use ->
class Tester
{
public $foo;
const BLAH;
public static function bar(){}
}
$t = new Tester;
$t->foo;
Tester::bar();
Tester::BLAH;
这篇关于PHP类:什么时候使用:: vs. - > ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文