PHP::(这是什么意思/做什么?)

PHP::(这是什么意思/做什么?),php,Php,谷歌有点难:“因为它忽略了符号 因此,以一种令人振奋的方式,我试图找出::适合PHP的位置 谢谢这意味着静态方法 Product::get_matching_products($keyword); 这意味着get\u matching\u products是Product上的静态方法。双冒号是静态方法调用 以下是静态方法的PHP手册页面: 而且。简单地说,您可以从代码的任何部分调用静态方法或变量,而无需实例化类。为了实现这一点,您可以使用: 以下是帮助您从他们的手册中获得帮助的示例 <?

谷歌有点难:“因为它忽略了符号

因此,以一种令人振奋的方式,我试图找出::适合PHP的位置


谢谢

这意味着静态方法

Product::get_matching_products($keyword);

这意味着
get\u matching\u products
Product
上的静态方法。双冒号是静态方法调用

以下是静态方法的PHP手册页面:


而且。

简单地说,您可以从代码的任何部分调用静态方法或变量,而无需实例化类。为了实现这一点,您可以使用:

以下是帮助您从他们的手册中获得帮助的示例

<?php
function Demonstration()
{
    return 'This is the result of demonstration()';
}

class MyStaticClass
{
    //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error
    public static $MyStaticVar = null;

    public static function MyStaticInit()
    {
        //this is the static constructor
        //because in a function, everything is allowed, including initializing using other functions

        self::$MyStaticVar = Demonstration();
    }
} MyStaticClass::MyStaticInit(); //Call the static constructor

echo MyStaticClass::$MyStaticVar;
//This is the result of demonstration()
?> 

:vs.->,self vs.this

对于那些对::->自我之间的区别感到困惑的人,我提出以下规则:

如果引用的变量或方法声明为const或static,则必须使用运算符

如果引用的变量或方法未声明为常量或静态,则必须使用->运算符

如果要从类中访问常量、静态变量或方法,则必须使用自引用self

如果从非常量或静态的类中访问变量或方法,则必须使用自引用变量$this