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

The difference between -> and :: in PHP

藏色散人
Release: 2023-04-08 09:00:01
forward
3915 people have browsed it

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;
Copy after login

::

Reference methods of static methods and static properties in classes

For example:

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

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静态属性的值
Copy after login

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!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template