PHP面向对象问题

spg120 2011-03-10 08:06:56
Warning: The magic method __get() must have public visibility and cannot be static in D:\wamp\www\1\Person.class.php on line 44

Warning: The magic method __set() must have public visibility and cannot be static in D:\wamp\www\1\Person.class.php on line 53

Warning: The magic method __isset() must have public visibility and cannot be static in D:\wamp\www\1\Person.class.php on line 88

Warning: The magic method __unset() must have public visibility and cannot be static in D:\wamp\www\1\Person.class.php on line 94

我看lamp教程上面没报警告,我这个怎么报警告了

还有这么用到底对不对?

源码:
class Person {
private $name;
private $age;
private $sex;

function __construct($name="", $age=0, $sex="男"){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}

private function __get($proName){
if($proName=="age"){
if($this->age>40)
return $this->age-10;
}

return $this->$proName;
}

private function __set($proName, $proValue){
if($proName=="age"){
if($proValue > 100 || $proValue < 0)
return;
}

$this->$proName=$proValue;
}


function say(){
echo "我的名子:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>";
}

function run(){
$this->left();
$this->left();
$this->right();
$this->right();
$this->gogo();
$this->gogo();
$this->gogo();
}

private function left(){
echo "LLLLLLLLLLLLL<br>";
}
private function right(){
echo "RRRRRRRRRRRRRR<br>";
}

private function gogo(){
echo "GGGGGGGGGGGGGGGGG<br>";
}

private function __isset($proName){
if($proName=="age")
return false;
return isset($this->$proName);
}

private function __unset($proName){
unset($this->$proName);
}

function eat(){

}

function __destruct(){
echo $this->name."再见!<br>";
}
}



$p1=new Person("zhangsan", 80, "女");
$p2=new Person("lisi", 25);
$p3=new Person("wangwu");

unset($p1->name);


if(isset($p1->name)){
echo "存在<br>";
}else{
echo "没有这个成员 <br>";
}
...全文
403 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
q906475407 2012-06-08
  • 打赏
  • 举报
回复
别人能运行正常,那就是自我的问题,这也是一种经验的积累。偶尔出现一些问题可以加深记忆,太多了就怀疑你能力了,
li55555558888 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

哄哄,楼主这个例子俺看过,那本叫《细说php》来着,果然误人子弟的例子。
[/Quote]


也不要那样说。一本书有一点bug也没有什么。总得来说还是可以的。我的了多部php的书。也没有那本比它好很多。
miantiao522 2011-08-03
  • 打赏
  • 举报
回复
哄哄,楼主这个例子俺看过,那本叫《细说php》来着,果然误人子弟的例子。
ImN1 2011-03-13
  • 打赏
  • 举报
回复
手册上面写的很清楚,魔术方法都要public

楼上没报错估计是ini里面把错误报告的选项关闭了
yuanjiulin 2011-03-12
  • 打赏
  • 举报
回复
我运行怎么没报错呀?
spg120 2011-03-12
  • 打赏
  • 举报
回复
这节讲的是面向对象封装,说的是
有些东西封装起来就能成为一个整体这个意思我自学没多长时间,我知道只要把private改成public就对了,但以后再遇到这种情况该怎么办呢?哪种是对啊?
helloyou0 2011-03-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 spg120 的回复:]

绝对没看错,lamp兄弟连的教程
[/Quote]


正常啊, 只是个warning,他们把错误提示关了....

给个链接, 让我们去嘲笑嘲笑.... :)





spg120 2011-03-10
  • 打赏
  • 举报
回复
绝对没看错,lamp兄弟连的教程
q344617263 2011-03-10
  • 打赏
  • 举报
回复
魔术方法__get() 必须声明为public 并且不能是静态的
ImN1 2011-03-10
  • 打赏
  • 举报
回复
哪个误人子弟的教程?

不会是你看漏了写着“这是错误的例子”吧?

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧