Step by step, I am migrating my project(s) from PHP 7.1 to PHP 8.0.
In the official PHP manual, in the subchapter "Deprecated Features" of the chapter "Migrating from PHP 7.3.x to PHP 7.4.x", I tried to understand the following deprecation description:
Unbinding
$this
when$this
is usedUnbinding
$this
of a non-static closure that uses$this
is deprecated.
Though, without success.
That's why I would appreciate, if someone could explain me in detail, what this means. Maybe a code snippet could be helpful too.
Thank you very much for your time!
Maybe this helps for the explanation too:
In my project, I have only one situation to which, in my opinion, this deprecation notice could be appliable: to the method executeGroupHandler
of the RouteCollection
class presented bellow. I preferred to paste a bit more code of the class, in order to help you understand the context in which I use the method executeGroupHandler
.
RouteCollection class:
<?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);
}
}
Use of RouteCollection class:
Having the RouteCollection
class defined, I use it similar to the following:
<?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'],
]);
});
//...