Home > Backend Development > PHP Tutorial > $this->name和$this->$name的区别,我糊涂了!

$this->name和$this->$name的区别,我糊涂了!

WBOY
Release: 2016-06-23 13:47:08
Original
978 people have browsed it

在set和get方法中使用的是$this->$name,我一看就蒙了,平时给属性赋值$this->name='sss'; 求详细介绍和解释!!!!!


回复讨论(解决方案)

后者是变量
前者是变量的变量

$name=aaa;
$this->$name 等於 $this->aaa;
$this->aaa是??量

class a{ private $name; $this->name; private $$name; $this->$name;}
Copy after login

我明白了,发完帖子之后想通了,其实就是传递进来一个变量,实际上我要给变量的变量赋值。

<?php 	         class classname{		private $attribute;						function __get($name)		{			echo "__get"."<br />";			return $this->$name."<br/>";		}		function __set($name,$value)		{			echo "__set"."<br />";			$this->$name=$value;					}	}	$a = new classname;	$a->attribute = 90;		echo $a->attribute;		?>
Copy after login

此时输出:
__set
__get
90
但是把类外语句改为:
$a->$attribute = 90;
echo $a->$attribute;(或echo $a->attribute;)
就只输出:
__set
__get

何解呢?

PHP code?12345678910111213141516171819"……


噢噢~~~明白了....奇怪,也是再回帖之后再想想就明白了。

对于5楼同样不解

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