請各位幫我看看我的 private $isHungry=true;為什么不提示呢?不是應(yīng)該訪問不到么?
<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("PRC");
class Human
{
? ? public $name;
public $weight;
//受保護(hù)的類成員,只有自身和其子類可以訪問到
protected $height;
//也是受保護(hù)的成員,只能被自身訪問
? ? private $isHungry=true;
public function eat($food)
{
echo $this->name."'s eating ".$food."<br>";
}
}
class NbaPlayer extends Human
{
public $team='';
public $PlayNum='';
//受保護(hù)的類成員,只能別自身訪問到,當(dāng)public function getAge()自身定義了這個(gè)方法時(shí),就可以被自身訪問到
private $age='50';
function __construct($name,$height,$weight,$team,$PlayNum)
{
echo"construct方法被調(diào)用<br>";
? ?$this->name=$name;
? ?$this->height=$height;
$this->weight=$weight;
$this->team=$team;
? ?$this->PlayNum=$PlayNum;
echo $this->isHungry."\n";
}
function __destruct()
{ echo"destruct方法被調(diào)用.$this->name.<br>";
}
? ?//定義方法[跟原來定義函數(shù)是一樣的]
public function run()
{
echo'Running<br>';
}
public function jump()
{
echo'Jumping<br>';
}
//受保護(hù)的類成員age可以被自身調(diào)用
public function getAge()
{
echo $this->name."'is age is ".($this->age)."\n";
}
}
$wang=new NbaPlayer("wang","160cm","50kg","Bull","56");
//echo $wang->getAge()."\n";
//echo $wang->height;//提示不能訪問
?>
2015-09-11
你打開php.ini配置文件 搜索error_reporting =?
將后面的值改為E_ALL
然后就會(huì)報(bào)notice錯(cuò)誤
construct方法被調(diào)用
Notice: Undefined property: NbaPlayer::$isHungry in?D:\phpStudy\WWW\test.php?on line?33
destruct方法被調(diào)用.wang.
2015-09-17
然而我的也沒有提示-。-我還把php.ini里面的display_error打開了……
2015-09-10
private 屬性和方法不會(huì)被子類繼承,只能在自身里面訪問