PHP属性访问和美元符号


PHP property access and dollar sign

我最近一直在加紧我的PHP游戏。来自JavaScript,我发现对象模型更容易理解。

我遇到了一些怪癖,我想澄清一下,我似乎在文档中找不到。

在PHP中定义类时,可以这样定义属性:

class myClass {
    public $myProp = "myProp";
    static $anotherProp = "anotherProp";
}

对于$myProp的公共变量,我们可以使用$myClass->myProp访问它(假设myClass在一个名为$myClass的变量中被引用),而不使用美元符号。

我们只能使用::访问静态变量。因此,我们可以使用美元符号访问静态变量,如$myClass::$anotherProp

问题是,为什么我们必须使用::而不是->的美元符号?

编辑

这是我认为可以工作的代码:

class SethensClass {
    static public $SethensProp = "This is my prop!";
}
$myClass = new SethensClass;
echo $myClass::$SethensProp;

使用::作用域操作符访问类常量,而没有美元符号,因此需要$来区分静态类属性和类常量。

class myClass {
  public static $staticProp = "static property";
  const CLASS_CONSTANT = 'a constant';
}
echo myClass::CLASS_CONSTANT;
echo myClass::$staticProp;

所以要访问一个变量,$是必需的。但是$不能像$myClass::staticProp一样放在类名的开头,因为这样解析器就无法识别类名,因为也可以使用变量作为类名。因此,它必须附加到属性。

$myClass = "SomeClassName";
// This attempts to access a constant called staticProp
// in a class called "SomeClassName"
echo $myClass::staticProp;
// Really, we need
echo myClass::$staticProp;

贪心!

有时在基类中引用函数和变量,或者在还没有任何实例的类中引用函数是有用的。操作符用于此操作。

<?php
class A
{
    function example()
    {
        echo "I am the original function A::example().<br>'n";
    }
}
class B extends A
{
    function example()
    {
        echo "I am the redefined function B::example().<br>'n";
        A::example();
    }
}
// there is no object of class A.
// this will print
//   I am the original function A::example().<br>
A::example();
// create an object of class B.
$b = new B;
// this will print 
//   I am the redefined function B::example().<br>
//   I am the original function A::example().<br>
$b->example();
?> 

上面的例子调用了A类中的函数example(),但是没有A类的对象,所以我们不能写$ A ->example()或类似的东西。相反,我们将example()作为"类函数"调用,也就是说,作为类本身的函数,而不是该类的任何对象。

有类函数,但没有类变量。实际上,在调用时根本没有对象。因此,类函数可以不使用任何对象变量(但它可以使用局部和全局变量),也可以根本不使用$this。

在上面的例子中,类B重新定义了函数example()。类A中的原始定义被遮蔽,不再可用,除非您使用::-操作符特别引用类A中example()的实现。编写A::example()来完成此操作(实际上,您应该编写parent::example())。

在这个上下文中,有一个当前对象,它可能有对象变量。因此,当在对象函数内使用时,可以使用$this和对象变量。