在基类php中重写函数
在一个实例中,例如以下哪一个被称为在基类php中重写函数,php,Php,在一个实例中,例如以下哪一个被称为 这是我们的基础课 <?php class baseService { public function __construct() { //constructor code } public function handleRawRequest($_SERVER, $_GET, $_POST) { $this->handleRequest($url, $method, $parameters, $requ
这是我们的基础课
<?php
class baseService
{
public function __construct()
{
//constructor code
}
public function handleRawRequest($_SERVER, $_GET, $_POST)
{
$this->handleRequest($url, $method, $parameters, $requestBody, $accept);
}
public function handleRequest($url, $method, $parameters, $requestBody, $accept)
{
switch($method)
{
case 'GET':
$this->performGet($url, $parameters, $requestBody, $accept);
break;
default:
$this->notImplementedResponse();
}
}
public function performGet($url, $parameters, $requestBody, $accept)
{
$this->methodNotAllowedResponse();
}
protected function methodNotAllowedResponse()
{
// out put some sort of 405 (Method Not Allowed)
}
}
?>
我们将其扩展到特定的服务,以提供特定的功能
<?php
require "baseService.php";
class betterService extends baseService
{
public function __construct()
{
// stuff
}
public function performGet($url, $parameters, $requestBody, $accept)
{
//do some stuff
}
}
?>
然后,使用另一个php文件,我们创建了一个更好的服务,并向其发送一些请求,如下所示:
<?php
require "NameRestService.php";
$service = new NameRestService();
$service->handleRawRequest($_SERVER, $_GET, $_POST);
?>
我们是否需要做一些事情,或者该方法不允许返回消息?
如果它确实是第二个,那么如何将其更改为do stuff?将$this替换为self::以从调用类执行该方法
self::performGet(...
您可以使用另一个标志参数来标识要重载的参数。哦?你能解释一下怎么做吗?它会称孩子为更好的服务和“做一些事情”的方法。如果需要,您不必更改任何内容。
$this->performGet()
调用子betterService
中的重写方法,无需使用self::performGet()
。