<?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
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;/**
另外:以后发代码,格式化了吧
*/
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;
?>
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();
?>
是因为你第10行有个大写分号,为什么没输出“猪”
试试
function setInfo($name){return $this->name;}//编译器提示警告
改成
function setInfo($name){$this->name = $name;}//编译器提示警
你的setInfo函数并没有给$name属性赋值,而是直接返回了$this->name
function setInfo($name){$this->name = $name;}
这里没有任何赋值操作,所以仍然为原值(空)
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;
private $name;
function setInfo($name)
{
$this->name = $name;
}function getInfo()
{
return $this->name;
}}$pig=new animal();
$pig->setInfo('lizhi');
$name=$pig->getInfo();//编译器提示错误
echo $name;