在 PHP 中扩展抽象类时出现致命错误
我正在学习高级 OOP PHP..(或者我想学习:))
摘录:
<?php
abstract class Karakter
{
abstract public function isim($name);
abstract public function yas($age);
public function yazdir()
{
print $this->isim() . " " . $this->yas();
}
}
class Insan extends Karakter
{
public $isim;
public $yas;
public function isim()
{
return "Bu adamın ismi: " . $this->isim;
}
public function yas()
{
return "Bu adamın yaşı: " . $this->yas;
}
}
当我运行这段代码时,我无法获胜。我可以看到这个错误:
Fatal error: Declaration of Insan::isim() must be compatible with that of Karakter::isim() in C:\AppServ\www\OOP\1.php on line 26
I'm learning advanced level of OOP PHP..(Or I want to learn :))
This is my code.
Excerpt:
<?php
abstract class Karakter
{
abstract public function isim($name);
abstract public function yas($age);
public function yazdir()
{
print $this->isim() . " " . $this->yas();
}
}
class Insan extends Karakter
{
public $isim;
public $yas;
public function isim()
{
return "Bu adamın ismi: " . $this->isim;
}
public function yas()
{
return "Bu adamın yaşı: " . $this->yas;
}
}
When I run this code I can't win through. I can see this error:
Fatal error: Declaration of Insan::isim() must be compatible with that of Karakter::isim() in C:\AppServ\www\OOP\1.php on line 26
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已在抽象类中使用一个参数定义了函数
imim
。为了在任何子类中正确实现此函数,您必须仅使用一个参数覆盖该函数:
You have defined the function
isim
in the abstract class with one parameter.In order to correctly implement this function in any subclass you must override the function with exactly one parameter:
在抽象类中,您定义了 isim() 来期望参数。但在延伸类中,你却没有遵循这个规则。
这是定义:
但是你可以像这样扩展它,不带参数:
In your abstract class, you defined
isim()
to expect a parameter. But in the extending class, you did not follow this rule.This is the definition:
But then you extend it like this, without a parameter: