QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1272|回复: 9

为什么出错?PHP代码

[复制链接]
发表于 2004-11-12 16:28:37 | 显示全部楼层 |阅读模式
<?php
class 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 .
发表于 2004-11-12 16:57:00 | 显示全部楼层
var $name?
没见过这么写的……
你不会是在写asp吧?
回复

使用道具 举报

 楼主| 发表于 2004-11-12 16:59:04 | 显示全部楼层
晕,类的成员要用VAR来声明!
回复

使用道具 举报

发表于 2004-11-12 17:09:07 | 显示全部楼层
class 里的变量不是这么用吧?

我也记不清具体怎么用了,好像不能直接使用。
回复

使用道具 举报

 楼主| 发表于 2004-11-12 20:03:40 | 显示全部楼层
不会吧,我这是照书上抄的,那请问PHP4.1和现在最新的版本在语法上有什么区别吗?
回复

使用道具 举报

发表于 2004-11-12 20:36:40 | 显示全部楼层
$this->name

你试试可以么?
回复

使用道具 举报

发表于 2004-11-28 12:21:51 | 显示全部楼层
把function show_name()
改为function show_name($name)
就可以了
回复

使用道具 举报

发表于 2004-11-28 13:17:37 | 显示全部楼层
[quote:7113ac51f2="minghacker"]把function show_name()
改为function show_name($name)
就可以了[/quote]
???????
回复

使用道具 举报

发表于 2004-11-28 18:56:42 | 显示全部楼层
是哪本书呀

兔兔写的,可以显示the name passed to this method is alicecheng .
[code:1]
<?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);
?>
[/code:1]
回复

使用道具 举报

发表于 2004-11-28 19:24:52 | 显示全部楼层
我想楼主的这种写法应该是要求不需要给函数添加参数就可以显示 $name 的………
这个在许多 class 里是很长用的~~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-2 08:37 , Processed in 0.128283 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表