为什么出错?PHP代码
<?phpclass printname
{
var $name;
function show_name()
{
echo "\n";
echo "the name passed to this method is $name.", "\n";
}
}
$obj = new printname;
$obj -> name="alicecheng";
$obj -> show_name();
?>
网页显示:
Notice: Undefined variable: name in f:\php\index.php on line 8
the name passed to this method is . var $name?
没见过这么写的……
你不会是在写asp吧? 晕,类的成员要用VAR来声明! :shock: class 里的变量不是这么用吧?
我也记不清具体怎么用了,好像不能直接使用。 不会吧,我这是照书上抄的,那请问PHP4.1和现在最新的版本在语法上有什么区别吗? $this->name
你试试可以么? 把function show_name()
改为function show_name($name)
就可以了 把function show_name()
改为function show_name($name)
就可以了
??????? 是哪本书呀 :mrgreen:
兔兔写的,可以显示the name passed to this method is alicecheng .
<?php
class Printname
{
var $classname = "Printname";
var $name;
function show_name($name = "") {
echo "\n";
echo "the name passed to this method is $name .\n";
}
}
$p = new Printname();
$p->name="alicecheng";
$p->show_name($p->name);
?>
我想楼主的这种写法应该是要求不需要给函数添加参数就可以显示 $name 的………
这个在许多 class 里是很长用的~~~
页:
[1]