Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP property access and dollar sign

Tags:

php

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;
like image 582
Sethen Avatar asked Mar 12 '13 16:03

Sethen


People also ask

What does the dollar sign mean in PHP?

The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value. These are called variable variables in PHP.

Can PHP variables work without using the dollar sign ($) symbol?

Nope. Variables in PHP must start with $ . The other approach is to use constants. Constants aren't "another approach" to variables if you need to modify the contents of the variable.

What are the properties of PHP?

In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP. Value of property can be different for each instance of class. That's why it is sometimes referred as instance variable.

What is the use of symbol in PHP explain it with an example?

The @ symbol is the error control operator ("silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.


1 Answers

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;
like image 140
Michael Berkowski Avatar answered Oct 24 '22 06:10

Michael Berkowski