PHP中使用echo提示变量未定义的问题
代码段:<?phpclassA{public$attribute="defaultvalue";functionoperation(){echo"Something<br...
代码段:
<?php
class A
{
public $attribute="default value";
function operation()
{
echo "Something<br/>";
echo "The value of \$attribute is:".$attribute."<br/>";
}
}
$a=new A();
echo $a->attribute."<br/>";
$a->operation();
?>
效果是:
default value
Something
Notice: Undefined variable: attribute
in C:\Users\ycgs\Desktop\PHPnow-1.5.6\htdocs\6\127.php on line
8
The value of $attribute is:
问题:首先定义了一个类,然后用new创建实例,echo $a->attribute."<br/>";显示default value表明实例创建成功,但是 echo "The value of \$attribute is:".$attribute."<br/>";却提示attribute未定义,明明已经在类中写明并创建了实例了啊? 展开
<?php
class A
{
public $attribute="default value";
function operation()
{
echo "Something<br/>";
echo "The value of \$attribute is:".$attribute."<br/>";
}
}
$a=new A();
echo $a->attribute."<br/>";
$a->operation();
?>
效果是:
default value
Something
Notice: Undefined variable: attribute
in C:\Users\ycgs\Desktop\PHPnow-1.5.6\htdocs\6\127.php on line
8
The value of $attribute is:
问题:首先定义了一个类,然后用new创建实例,echo $a->attribute."<br/>";显示default value表明实例创建成功,但是 echo "The value of \$attribute is:".$attribute."<br/>";却提示attribute未定义,明明已经在类中写明并创建了实例了啊? 展开
1个回答
2014-09-03
展开全部
echo "The value of \$attribute is:".$attribute."<br/>";
改成
echo "The value of \$attribute is:".$thisp->attribute."<br/>";
改成
echo "The value of \$attribute is:".$thisp->attribute."<br/>";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询