php 后期静态绑定revieve 错误期待T_FUNCTION
我是 OOP 新手,我一直在研究这个示例,但我似乎无法摆脱这个错误
解析错误:语法错误,意外的“;”,期望 C:\Program Files (x86)\Apache 中的 T_FUNCTION Software Foundation\Apache2.2\...\php_late_static_bindings.php on line 16
我试图执行以下代码:
abstract class father {
protected $lastname="";
protected $gender="";
function __construct($sLastName){
$this->lastname = $sLastName;
}
abstract function getFullName();
public static function create($sFirstName,$sLastName){
return new self($sFirstName,$sLastName);
};
}
class boy extends father{
protected $firstname="";
function __construct($sFirstName,$sLastName){
parent::__construct($sLastName);
$this->firstname = $sFirstName;
}
function getFullName(){
return("Mr. ".$this->firstname." ".$this->lastname."<br />");
}
}
class girl extends father{
protected $firstname="";
function __construct($sFirstName,$sLastName){
parent::__construct($sLastName);
$this->firstname = $sFirstName;
}
function getFullName(){
return("Ms. ".$this->firstname." ".$this->lastname."<br />");
}
}
$oBoy = boy::create("John", "Doe");
print($oBoy->getFullName());
有人有什么想法吗? $oGirl = girls::create("Jane", "Doe"); print($oGirl->getFullName());
I am new to OOP and I have been working on this example but I cannot seem to get rid of this error
Parse error: syntax error, unexpected ';', expecting T_FUNCTION in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\...\php_late_static_bindings.php on line 16
I was attempting to execute the follow code:
abstract class father {
protected $lastname="";
protected $gender="";
function __construct($sLastName){
$this->lastname = $sLastName;
}
abstract function getFullName();
public static function create($sFirstName,$sLastName){
return new self($sFirstName,$sLastName);
};
}
class boy extends father{
protected $firstname="";
function __construct($sFirstName,$sLastName){
parent::__construct($sLastName);
$this->firstname = $sFirstName;
}
function getFullName(){
return("Mr. ".$this->firstname." ".$this->lastname."<br />");
}
}
class girl extends father{
protected $firstname="";
function __construct($sFirstName,$sLastName){
parent::__construct($sLastName);
$this->firstname = $sFirstName;
}
function getFullName(){
return("Ms. ".$this->firstname." ".$this->lastname."<br />");
}
}
$oBoy = boy::create("John", "Doe");
print($oBoy->getFullName());
Does anyone have any ideas?
$oGirl = girl::create("Jane", "Doe");
print($oGirl->getFullName());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先必须删除方法定义后面的分号:
然后,您可能想要使用
static
,而不是self
,这里:解释:
self
指向该类其中写的是——这里是father
类,它是抽象的,无法实例化。static
表示后期静态绑定,并且在这里将指向您的boy
类;这就是您想要实例化的那个。You first have to remove the semi-colon that's after your method definition :
Then, you probably want to use
static
, and notself
, here :Explanation :
self
points to the class in which it is written -- here, thefather
class, which is abstract, and cannot be instanciated.static
, on the other hand, means late static binding -- and, here, will point to yourboy
class ; which is the one you want to instanciate.PHP 的错误报告通常相当不错。只需阅读错误即可。问题就在这里:
删除训练分号。
PHP's error reporting is usually pretty good. Just read the error. The problem is here:
Remove the training semicolon.