doudiemei2013 2011-09-23 11:03
浏览 80
已采纳

美元符号在PHP中意味着什么? [关闭]

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

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

Why are there no dollar signs before these variables?

  • 写回答

7条回答 默认 最新

  • dongqiu5184 2011-09-23 11:12
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan