What are the different ways where we can use object operators ->
in PHP?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
PHP has two object operators.
The first, ->
, is used when you want to call a method on an instance or access an instance property.
The second, ::
, is used when you want to call a static
method, access a static
variable, or call a parent class's version of a method within a child class.
回答2:
When accessing a method or a property of an instantiated class
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$a = new SimpleClass();
echo $a->var;
$a->displayVar();
回答3:
Call a function:
$foo->bar();
Access a property:
$foo->bar = 'baz';
where $foo
is an instantiated object.
回答4:
It is used when referring to the attributes of an instantiated object. e.g:
class a {
public $yourVariable = 'Hello world!';
public function returnString() {
return $this->yourVariable;
}
}
$object = new a();
echo $object->returnString();
exit();
标签:
Ta的文章
更多文章
0条评论
还没有人评论过~