Home > Backend Development > PHP Tutorial > What does :: mean in php

What does :: mean in php

下次还敢
Release: 2024-04-27 17:36:53
Original
1121 people have browsed it

Double colon (::) is used in PHP for: 1. Namespace delimiter; 2. Static method call; 3. Parent class access; 4. Constant access; 5. Delayed static binding. For example, MyNamespace::MyClass represents the namespace delimiter, while MyClass::myStaticMethod() represents a static method call.

What does :: mean in php

Double colon (::) in PHP

Double colon (::) in PHP has the following Purpose:

1. Namespace separator

  • In PHP 5.3 and later versions, double colons are used to separate classes and functions in the namespace and constants.
  • For example: MyNamespace::MyClass

2. Static method call

  • Double colon is available For calling static methods of a class without having to instantiate the class.
  • For example: MyClass::myStaticMethod()

##3. Parent class access

    Double The colon can be used to access the properties and methods of the parent class.
  • For example:
  • parent::myProperty

4. Constant access

    Double colon can be used Access the constants of a class without having to go through the class name.
  • For example:
  • MyClass::MY_CONSTANT

5. Delayed static binding (LSB)

    Double colons can be used to implement delayed static binding, allowing the method to be called to be determined at runtime.
  • For example:
  • static::myMethod()

Example:

<code class="php">// 命名空间分隔符
namespace MyNamespace;
class MyClass {}

// 静态方法调用
class Foo {
    public static function myStaticMethod() {
        echo "This is a static method.\n";
    }
}
Foo::myStaticMethod();

// 父类访问
class Bar extends Foo {
    public function myChildMethod() {
        echo "This is a child method.\n";
        parent::myStaticMethod();
    }
}
$bar = new Bar();
$bar->myChildMethod();

// 常量访问
class Baz {
    const MY_CONSTANT = 'Hello, world!';
}
echo Baz::MY_CONSTANT;</code>
Copy after login

The above is the detailed content of What does :: mean in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template