<?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属性?
2 类employee里$ein并没有被赋值 . 加一句var_dump($this->$ein); 结果为null.
你举得例子内的$ein自始至终都是作为方法内的局部变量而不是类属性存在 .
public function gettitle()
{
$this->title = "EASY php websites with the zend framework";//本来有值被替换了
print "title: {$this->title}<br/>";//输出当然不对了
}本回答被网友采纳