PHP property access and dollar sign
php
Solution
A class constant is accessed with the `::` scope operator, and no dollar sign, so the `$` is needed there to differentiate between static class properties and class constants.
class myClass {
public static $staticProp = "static property";
const CLASS_CONSTANT = 'a constant';
}
echo myClass::CLASS_CONSTANT;
echo myClass::$staticProp;
So to access a variable, the `$` is necessary. But the `$` cannot be placed at the beginning of the class name like `$myClass::staticProp` because then the class name could not be identified by the parser, since it is also possible to use a variable as the class name. It must therefore be attached to the property.
$myClass = "SomeClassName";
// This attempts to access a constant called staticProp
// in a class called "SomeClassName"
echo $myClass::staticProp;
// Really, we need
echo myClass::$staticProp;
Problem
I have been stepping up my PHP game lately. Coming from JavaScript, I've found the object model to be a little simpler to understand. I've run into a few quirks that I wanted some clarifying on that I can't seem to find in the documentation. When defining classes in PHP, you can define properties like so: ``` class myClass { public $myProp = "myProp"; static $anotherProp = "anotherProp"; } ``` With the public variable of `$myProp` we can access it using (assuming `myClass` is referenced in a variable called `$myClass`) `$myClass->myProp` without the use of the dollar sign. We can only access static variables using `::`. So, we can access the static variable like `$myClass::$anotherProp` with a dollar sign. Question is, why do we have to use dollar sign with `::` and not `->`?? EDIT This is code I would assume would work (and does): ``` class SethensClass { static public $SethensProp = "This is my prop!"; } $myClass = new SethensClass; echo $myClass::$SethensProp; ```