【发布时间】:2021-05-07 22:05:19
【问题描述】:
我正在逐步将我的项目从 PHP 7.1 迁移到 PHP 8.0。
在 PHP 官方手册中,在 “从 PHP 7.3.x 迁移到 PHP 7.4.x”一章的子章节 “弃用的功能” 中,我尝试了了解以下deprecation description:
在使用
$this时取消绑定$this取消绑定使用
$this的非静态闭包的$this已弃用。
虽然没有成功。
这就是为什么我会很感激,如果有人可以详细解释我,这意味着什么。也许代码 sn-p 也会有所帮助。
非常感谢您的宝贵时间!
也许这也有助于解释:
在我的项目中,我认为只有一种情况适用此弃用通知:适用于下面介绍的RouteCollection 类的方法executeGroupHandler。为了帮助您了解我使用方法executeGroupHandler 的上下文,我更愿意粘贴更多该类的代码。
RouteCollection 类:
<?php
namespace Packages\Router;
//...
use Packages\Router\RouteCollectionInterface;
/**
* Route collection.
*/
class RouteCollection implements RouteCollectionInterface {
//...
/**
* Group patterns list. Indexed array.
*
* Each time a group handler is executed its pattern is saved in this list.
* All addRoute operations taken place inside the scope of a group handler
* prefix the pattern of the corresponding route with the saved group pattern.
*
* @var array
*/
private $groupPatterns = [];
//...
/**
* Add a group (helper method).
*
* @param string $pattern Group pattern.
* @param \Closure $handler Group handler.
* @return $this
*/
public function group(string $pattern, \Closure $handler) {
$this->addGroup($pattern, $handler);
return $this;
}
/**
* Add a group.
*
* @param string $pattern Group pattern.
* @param \Closure $handler Group handler.
* @return $this
*/
private function addGroup(string $pattern, \Closure $handler) {
$this->saveGroupPattern($pattern);
$this->executeGroupHandler($handler);
/*
* Remove the last group pattern from the group patterns list. This step
* is performed only after all calls for adding groups/routes inside the
* scope of the current group handler have finished their processing.
*/
$this->popLastGroupPattern();
return $this;
}
/**
* Save a group pattern.
*
* @param string $pattern Group pattern.
* @return $this
*/
private function saveGroupPattern(string $pattern) {
$this->groupPatterns[] = $pattern;
return $this;
}
/**
* Execute a group handler.
*
* Temporarily bind the group handler to the route collection
* object - defined by the argument in Closure::call - and
* execute it. Inside the scope of the group handler, the route
* collection will be accessed using the keyword "$this".
*
* @link https://www.php.net/manual/en/closure.call.php Closure::call
*
* @param \Closure $handler Group handler.
* @return mixed The return value of calling the handler.
*/
private function executeGroupHandler(\Closure $handler) {
return $handler->call($this);
}
/**
* Pop the group pattern off the end of group patterns list.
*
* @return string The popped group pattern.
*/
private function popLastGroupPattern() {
return array_pop($this->groupPatterns);
}
}
RouteCollection 类的使用:
定义了RouteCollection 类,我使用它类似于以下内容:
<?php
use Packages\Router\RouteCollection;
use SampleMvc\App\View\Template\Users\AddUser as AddUserView;
use SampleMvc\App\Controller\Users\AddUser as AddUserController;
$routeCollection = new RouteCollection();
// Add a group of routes to the route collection.
$routeCollection->group('/users/add', function() {
$this->get('', [AddUserView::class, 'index']);
$this->post('', [
'controller' => AddUserController::class,
'view' => [AddUserView::class, 'addUser'],
]);
});
//...
【问题讨论】: