Where do we use the object operator "

有哪些不同的方式,我们可以使用对象运营商->在PHP?


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.


访问实例化类的方法或属性时

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();

Call a function:

$foo->bar();

Access a property:

$foo->bar = 'baz';

where $foo is an instantiated object.

链接地址: http://www.djcxy.com/p/1762.html

上一篇: PHP语法$ var1是什么?

下一篇: 我们在哪里使用对象操作符“