Why do we have the dollar sign before every php variable?

What does the dollar sign mean in php in regards to oop?

  • Going through this code (sorry for the newb factor): <?php // Class class Building { // Object variables/properties public $number_of_floors = 5; // These buildings have 5 floors private $color; // Class constructor public function __construct($paint) { $this->color = $paint; } public function describe() { printf('This building has %d floors. It is %s in color.', $this->number_of_floors, $this->color ); } } // Build a building and paint it red $bldgA = new Building('red'); // Tell us how many floors these buildings have, and their painted color $bldgA->describe(); ?> It seems that the $ indicates a variable: $number_of_floors $color Make sense to me because each of those quantify something like "5" or "red". However I get confused when I see $bldgA = new Building('red'); Is the object $bldgA a variable? I get even more confused when I see the following: $bldgA->describe(); $bldgA->number_of_floors; Why is there no dollar sign before the variable $number_of_floors like so? $bldgA->$number_of_floors;

  • Answer:

    You are right, the $ is for variable. But in a class instance, you don't use $ anymore on properties because PHP would interpret and this can cause you an error. For example, if you use $bldgA->$number_of_floors; this will not return the $number_of_floors property of the object but PHP will first look at the value of $number_of_floors, let's say 3 for instance, so the previous line would be $bldgA->3; And that will give you an error

user784637 at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

$ is the way to refer to variables in PHP. Variables in PHP are dynamically typed, which means that their type is determined by what's assigned to them. Here's the page about http://php.net/manual/en/language.variables.php from the PHP manual. $a = "This is a string"; $b = 1; // This is an int $bldgA = new Building('red'); // bldgA is a variable and an object (aka an instance) of class Building. $bldgA->describe(); // This calls describe(), which is a member function of class Building (remember that $bldgA was declared as being an object of class Building) $bldgA->number_of_floors; // number_of_floors is a data member of class Building. You can think of it as a variable inside a class, but since it's part of the class with a fixed name, you don't refer to it with $.

Anson

$bldgA = new Building('red'); in this case $bldgA is an object. $bldgA->describe(); calls the function describe() from the object $bldgA $bldgA->number_of_floors; acces the variable number_of_floors from the object $bldgA but you should really take a look at http://php.net/manual/en/language.oop5.basic.php

peko

Yes, that's variable with assigned instance of class to it. And when it object then youre calling/getting arguments like so. Read about OOP in PHP, please. It could be very handy for You and help you understand how it works :)

kkszysiu

The $bldgA is a variable for the class Building so you can access the class function by using $Building->function_name example: $foo = $bldgA->describe(); the $number_of_floors is a variable inside the class

Mohammed Shannaq

$bldgA->number_of_floors; Does not call a local variable but a property (it's like a local variable part of the class definition) of a class. However it is possible to call $bldgA->$property_name;where $property_name is a name of the property you want to call. This is called variable variables and something you probably should look into after you've grasped OOP basics.

kjetilh

When writing $bldgA = new Building('red'); you assign the variable $bldgA a newly created object of class Building. Objects are a possible type of variables. In general when you see $ it always refers to variables. $bldgA->number_of_floors; should be read as: access the property of the object in variable $bldgA

Martin Dimitrov

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.