Home  >  Article  >  Backend Development  >  The difference between -> and :: in PHP

The difference between -> and :: in PHP

藏色散人
藏色散人forward
2020-01-20 13:11:193815browse

The difference between -> and :: in PHP

->

Methods and attributes in a class used to reference class instances

For example:

class Test{
    function add(){return $this->var++;}
    var $var = 0;
}
$a = new Test;  //  实例化对象名称
echo $a->add();
echo $a->var;

::

Reference methods of static methods and static properties in classes

For example:

class Test{
    public static function test(){
    public static $test = 1;
   }
}

Static methods and static properties of a class can be used directly without instantiating the object (the method used is class name::static method name)

Test::test();  //  调用静态方法test
Test::$test;  //  来取得$test静态属性的值

Note:

Static methods are used after reading this class or introducing them When this class file is created, it has already been instantiated and stored in memory. Non-static classes need to be new.

Even if a static class has multiple instances in memory, there is only one copy of the static attributes.

The above is the detailed content of The difference between -> and :: in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete