求帮忙?php oop例子
<?php
class animal{
public $name="";
public $color="";
public $age="";
function getInfo(){return $this->name;}
function setInfo($name){return $this->name;}//编译器提示警告
}
$pig=new animal();
$pig->setInfo('猪');
$name=$pig->getInfo();//编译器提示错误
echo $name;
?> PHP
[解决办法]
$pig=new animal();
$pig->setInfo('猪');//是这行错了,你的分号为全角的分号。
$name=$pig->getInfo();//编译器提示错误
echo $name;
/**
另外:以后发代码,格式化了吧
*/
<?php
class person{
private $name;
function setname($name){
return $this->name = $name;
}
function getname(){
return $this->name;
}
}
$p = new person();
$p->setname('lizhi');
echo $p->getname();
?>
class animal{
public $name="";
public $color="";
public $age="";
function getInfo(){
return $this->name;
}
function setInfo($name){
$this->name = $name; //这里应该是赋值,想必你复制错了
}
}
$pig=new animal();
$pig->setInfo('猪'); //这里原来是全角的;
$name=$pig->getInfo();
echo $name;