PHP 中,$this在一个方法中调用方法属性时,为什么是$this->$name,如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
abstract class cl1
{ private $name;
private $age;
private $x;
abstract function fun1();
abstract function fun2();
function __get($namex)
{ //问题提问出,不太懂?
return $this->$namex;
}
function __set($namex,$values)
{
$this->$namex=$values;
}
function ok()
{
echo $this->name." 年龄: ".$this->age."<br>";
}
}
class cl2 extends cl1
{
function __construct($name,$age)
{
$this->name=$name;
$this->age=$age;
}
function fun1()
{
echo "方法1";
}
function fun2()
{
echo "方法2";
}
function ok()
{
cl1::ok();
$this->x="XXX";
echo $this->x;
}
}
$p=new cl2("ainifls",22);
$p->ok();
?>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
abstract class cl1
{ private $name;
private $age;
private $x;
abstract function fun1();
abstract function fun2();
function __get($namex)
{ //问题提问出,不太懂?
return $this->$namex;
}
function __set($namex,$values)
{
$this->$namex=$values;
}
function ok()
{
echo $this->name." 年龄: ".$this->age."<br>";
}
}
class cl2 extends cl1
{
function __construct($name,$age)
{
$this->name=$name;
$this->age=$age;
}
function fun1()
{
echo "方法1";
}
function fun2()
{
echo "方法2";
}
function ok()
{
cl1::ok();
$this->x="XXX";
echo $this->x;
}
}
$p=new cl2("ainifls",22);
$p->ok();
?>
</body>
</html> 展开
2个回答
展开全部
举个简单例子
<?php
class Test{
public $name = "abc";
public $abc = "test";
public function Test(){
$name1 = "name";
echo $this->name; // 输出 abc
echo $this->$name1; // 输出 abc,因为 $name1 的值是name,相当与这里替换成 echo $this->name;
$name2 = $this->$name1; // $name2 的值是 abc
echo $this->$name2; // 输出 test,同上,相当与是 echo $this->abc;
}
}
<?php
class Test{
public $name = "abc";
public $abc = "test";
public function Test(){
$name1 = "name";
echo $this->name; // 输出 abc
echo $this->$name1; // 输出 abc,因为 $name1 的值是name,相当与这里替换成 echo $this->name;
$name2 = $this->$name1; // $name2 的值是 abc
echo $this->$name2; // 输出 test,同上,相当与是 echo $this->abc;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询