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}
";
}
public function getnumbercopies()
{
$this->copies = "5";
print "number copies available: {$this->copies}
";
}
}
$book = new book("0615303889");
?>
IE输出:
title: EASY php websites with the zend framework
number copies available: 5
实例化的数值没有输出?
而如下程序
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属性?
关于PHP的构造函数,请问如下程序为什么数值没有赋值给构造函数中的属性?
答案:3 悬赏:70
解决时间 2021-03-07 06:10
- 提问者网友:孤笛钟情你
- 2021-03-06 20:19
最佳答案
- 二级知识专家网友:甜野猫
- 2021-03-06 21:17
是你自己不小心把$this->title重新赋值了:
public function gettitle()
{
$this->title = "EASY php websites with the zend framework";//本来有值被替换了
print "title: {$this->title}
";//输出当然不对了
}
public function gettitle()
{
$this->title = "EASY php websites with the zend framework";//本来有值被替换了
print "title: {$this->title}
";//输出当然不对了
}
全部回答
- 1楼网友:野性且迷人
- 2021-03-07 00:01
1 类book构造函数里面没有输出操作.
2 类employee里$ein并没有被赋值 . 加一句var_dump($this->$ein); 结果为null.
你举得例子内的$ein自始至终都是作为方法内的局部变量而不是类属性存在 .
- 2楼网友:高冷不撩人
- 2021-03-06 22:32
例一:已知二次函数f(x)对任意x、y∈r都有f(x+y)=f(x)+f(y),且x>0时,f(x)<0,f(1)=-2
(1)判断函数f(x)的奇偶数。
(2)当x∈[-3,3]时,函数f(x)是否有最值?如果有,求出最值;如果没有,请说明理由。
解:令 x=y=0
得到f(0)=0
f(0)=f(x + -x)= f(x)+ f(-x) 奇函数
设 x10
f(x2)=f(x1+m)=f(x1)+f(m)
因为f(m)>0 f(m)<0
f(x2)0 既f(x1)-f(x2)>0
所以f(x)为减函数 故f(x)在【-2006,2006】上为减函数
所以f(x)max=f(-2006),f(x)min=f(2006)
赋值法一般就是令x.y为某值,代入所给的函数关系,也可以是抽象函数,一步步推导出想要的结果
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯