美元符号在PHP中意味着什么?

时间:2022-12-19 16:57:18

What does the dollar sign mean in PHP? I have this code:

美元符号在PHP中意味着什么?我有这个代码:

<?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 see the following:

但是当我看到以下情况时,我感到困惑:

$bldgA->describe();
$bldgA->number_of_floors;

Why are there no dollar signs before these variables?

为什么在这些变量之前没有美元符号?

7 个解决方案

#1


16  

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

你是对的,$是变量。但是在类实例中,你不再在属性上使用$,因为PHP会解释,这可能会导致错误。例如,如果您使用

$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

这不会返回对象的$ number_of_floors属性,但PHP会首先查看$ number_of_floors的值,比如说3,所以前一行是

$bldgA->3;

And that will give you an error

这会给你一个错误

#2


3  

$ 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.

$是在​​PHP中引用变量的方法。 PHP中的变量是动态类型的,这意味着它们的类型取决于分配给它们的内容。这是关于PHP手册变量的页面。

$a = "This is a string";

$ a =“这是一个字符串”;

$b = 1; // This is an int

$ b = 1; //这是一个int

$bldgA = new Building('red'); // bldgA is a variable and an object (aka an instance) of class Building.

$ bldgA = new Building('red'); // bldgA是一个变量和一个类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->描述(); //这调用describe(),它是类Building的成员函数(记住$ bldgA被声明为类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-> number_of_floors; // number_of_floors是类Building的数据成员。您可以将其视为类中的变量,但由于它是具有固定名称的类的一部分,因此您不会使用$引用它。

#3


2  

$bldgA = new Building('red');

in this case $bldgA is an object.

在这种情况下,$ bldgA是一个对象。

$bldgA->describe();

calls the function describe() from the object $bldgA

从对象$ bldgA调用函数describe()

$bldgA->number_of_floors;

acces the variable number_of_floors from the object $bldgA

从对象$ bldgA访问变量number_of_floors

but you should really take a look at php.net/manual/en/language.oop5.basic.php

但是你应该看一下php.net/manual/en/language.oop5.basic.php

#4


0  

The $bldgA is a variable for the class Building

$ bldgA是Building类的变量

so you can access the class function by using $Building->function_name

所以你可以使用$ Building-> function_name来访问类函数

example:

$foo = $bldgA->describe();

the $number_of_floors is a variable inside the class

$ number_of_floors是类中的变量

#5


0  

$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.

$ bldgA-> number_of_floors;不调用局部变量,而是调用类的属性(它类似于类定义的局部变量部分)。

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.

但是可以调用$ bldgA - > $ property_name;其中$ property_name是要调用的属性的名称。这被称为变量变量,您可能应该在掌握OOP基础知识后进行调查。

#6


0  

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 :)

是的,这是带有指定类的实例的变量。当它反对时,你就像这样调用/获取参数。请阅读PHP中的OOP。这对你来说非常方便,并帮助你理解它是如何工作的:)

#7


-1  

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.

写$ bldgA = new Building('red');为变量$ bldgA分配一个新创建的类Building对象。对象是一种可能的变量。

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

一般来说,当你看到$时,它总是指变量。 $ bldgA-> number_of_floors;应该读作:在变量$ bldgA中访问对象的属性

#1


16  

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

你是对的,$是变量。但是在类实例中,你不再在属性上使用$,因为PHP会解释,这可能会导致错误。例如,如果您使用

$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

这不会返回对象的$ number_of_floors属性,但PHP会首先查看$ number_of_floors的值,比如说3,所以前一行是

$bldgA->3;

And that will give you an error

这会给你一个错误

#2


3  

$ 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.

$是在​​PHP中引用变量的方法。 PHP中的变量是动态类型的,这意味着它们的类型取决于分配给它们的内容。这是关于PHP手册变量的页面。

$a = "This is a string";

$ a =“这是一个字符串”;

$b = 1; // This is an int

$ b = 1; //这是一个int

$bldgA = new Building('red'); // bldgA is a variable and an object (aka an instance) of class Building.

$ bldgA = new Building('red'); // bldgA是一个变量和一个类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->描述(); //这调用describe(),它是类Building的成员函数(记住$ bldgA被声明为类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-> number_of_floors; // number_of_floors是类Building的数据成员。您可以将其视为类中的变量,但由于它是具有固定名称的类的一部分,因此您不会使用$引用它。

#3


2  

$bldgA = new Building('red');

in this case $bldgA is an object.

在这种情况下,$ bldgA是一个对象。

$bldgA->describe();

calls the function describe() from the object $bldgA

从对象$ bldgA调用函数describe()

$bldgA->number_of_floors;

acces the variable number_of_floors from the object $bldgA

从对象$ bldgA访问变量number_of_floors

but you should really take a look at php.net/manual/en/language.oop5.basic.php

但是你应该看一下php.net/manual/en/language.oop5.basic.php

#4


0  

The $bldgA is a variable for the class Building

$ bldgA是Building类的变量

so you can access the class function by using $Building->function_name

所以你可以使用$ Building-> function_name来访问类函数

example:

$foo = $bldgA->describe();

the $number_of_floors is a variable inside the class

$ number_of_floors是类中的变量

#5


0  

$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.

$ bldgA-> number_of_floors;不调用局部变量,而是调用类的属性(它类似于类定义的局部变量部分)。

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.

但是可以调用$ bldgA - > $ property_name;其中$ property_name是要调用的属性的名称。这被称为变量变量,您可能应该在掌握OOP基础知识后进行调查。

#6


0  

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 :)

是的,这是带有指定类的实例的变量。当它反对时,你就像这样调用/获取参数。请阅读PHP中的OOP。这对你来说非常方便,并帮助你理解它是如何工作的:)

#7


-1  

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.

写$ bldgA = new Building('red');为变量$ bldgA分配一个新创建的类Building对象。对象是一种可能的变量。

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

一般来说,当你看到$时,它总是指变量。 $ bldgA-> number_of_floors;应该读作:在变量$ bldgA中访问对象的属性