关于PHP的构造函数,请问如下程序为什么数值没有赋值给构造函数中的属性?

<?php
class book
{
private $title;
private $isbn;
private $copies;

function __construct($isbn)
{
$this->setisbn($isbn);
$this->gettitle();
$this->getnumbercopies();
}
public function setisbn($isbn)
{
$this->isbn = $isbn;
}

public function gettitle()
{
$this->title = "EASY php websites with the zend framework";
print "title: {$this->title}<br/>";
}

public function getnumbercopies()
{
$this->copies = "5";
print "number copies available: {$this->copies}<br/>";
}
}
$book = new book("0615303889");
?>
IE输出:
title: EASY php websites with the zend framework
number copies available: 5
实例化的数值没有输出?
而如下程序
<?php
class employee
{
private $ein;
function __construct($ein)
{
if ($this->verifyein($ein)) {
echo "$ein verified.finish";
}
}
protected function verifyein($ein)
{
return true;
}
}
$employee = new employee("123-45-678");
?>
中为何123-45-678就会被赋值给类的 ein属性?

第1个回答  2013-09-12
1 类book构造函数里面没有输出操作.
2 类employee里$ein并没有被赋值 . 加一句var_dump($this->$ein); 结果为null.
你举得例子内的$ein自始至终都是作为方法内的局部变量而不是类属性存在 .
第2个回答  2016-06-08
是你自己不小心把$this->title重新赋值了:
public function gettitle()
{
$this->title = "EASY php websites with the zend framework";//本来有值被替换了
print "title: {$this->title}<br/>";//输出当然不对了
}本回答被网友采纳
第3个回答  2013-09-16
你自己没有输出的嘛。肯定没显示了~

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网