What does the dollar sign mean in PHP? I have this code:
<?php
class Building {
public $number_of_floors = 5;
private $color;
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
);
}
}
$bldgA = new Building('red');
$bldgA->describe();
?>
It seems that the $
indicates a variable like:
$number_of_floors
$color
But I get confused when I s开发者_开发技巧ee the following:
$bldgA->describe();
$bldgA->number_of_floors;
Why are there no dollar signs before these variables?
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
$
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 variables 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 $
.
$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 php.net/manual/en/language.oop5.basic.php
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
$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.
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 :)
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
精彩评论