php 后期静态绑定revieve 错误期待T_FUNCTION

发布于 2024-11-03 17:01:12 字数 1366 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-11-10 17:01:12

您首先必须删除方法定义后面的分号

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
} // there was a semi-colon, here

然后,您可能想要使用static,而不是self,这里:

public static function create($sFirstName,$sLastName){
    return new static($sFirstName,$sLastName);
}

解释:

  • self 指向该类其中写的是——这里是father 类,它是抽象的,无法实例化。
  • 另一方面,static 表示后期静态绑定,并且在这里将指向您的 boy 类;这就是您想要实例化的那个。

You first have to remove the semi-colon that's after your method definition :

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
} // there was a semi-colon, here

Then, you probably want to use static, and not self, here :

public static function create($sFirstName,$sLastName){
    return new static($sFirstName,$sLastName);
}

Explanation :

  • self points to the class in which it is written -- here, the father class, which is abstract, and cannot be instanciated.
  • static, on the other hand, means late static binding -- and, here, will point to your boy class ; which is the one you want to instanciate.
墨小墨 2024-11-10 17:01:12

PHP 的错误报告通常相当不错。只需阅读错误即可。问题就在这里:

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
};

删除训练分号。

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
}

PHP's error reporting is usually pretty good. Just read the error. The problem is here:

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
};

Remove the training semicolon.

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文