php面向对象的问题

WBOY
Release: 2016-06-23 13:45:42
Original
940 people have browsed it

请看下面一段代码,非常简单,请大家花点时间看看:

class person{                                                     public $name;    public function __construct(){        $this->$name='Tom';            //代码1     }}$a=new person();echo $a->name;
Copy after login


运行上面这段代码,会报一个'变量未定义'的错误.
请问为什么会报这个错误?
如果把代码1换成 $this->name='Tom',就不会报错
请问这里的美元符号有什么特殊意义吗?没有美元符号和有美元符号有什么区别?
谢谢.


回复讨论(解决方案)

那么你说说看:
$name 和 $$name 的区别

$this->name    类的name属性
$this->$name  类的$name 属性,由于你没先定义变量$name ,所以当然要报错了。

可变变量
如果$name = 'hello'
$this->$name   相当于  $this->hello

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template