Home  >  Article  >  Backend Development  >  What does :: mean in php

What does :: mean in php

下次还敢
下次还敢Original
2024-04-27 17:36:531065browse

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>

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!

Statement:
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
Previous article:What does ++ mean in phpNext article:What does ++ mean in php