Extending Controller

I have a class abastrata I hold the controller, that way I can call any container directly only using $ this-> namecontroller it is right to do, can bring a problem of performace?

------------------------------------------ Controller.php -----------------------------------

<?php namespace App\Action; use Slim\Container; abstract class Controller { protected $container; /* @var \Smarty $view*/ /* @var \App\Model\Loja $loja*/ /* @var \App\Model\Produto $produto*/ /* @var \App\Model\Banner $banner*/ /* @var \App\Model\Departamento $departamento*/ /* @var \App\Model\Paginacao $pager*/ /* @var \App\Model\Rating $rating*/ public function __construct(Container $container) { $this->container = $container; $smarty = $this->view->getSmarty(); $smarty->assign('menu', $this->loja->getMenu()); $smarty->assign('loja', $this->loja->getLoja()); $smarty->assign('cart', $this->loja->getCart()); } public function __get($key) { if ($this->container->has($key)) { return $this->container->{$key}; } } } ------------------------------------------ Produto.php ----------------------------------- <?php namespace App\Action; use App\Helper\Google; final class ProdutoAction extends Controller { public function dispatch($request, $response, $args) { $produto = $this->produto->getProduto($args['id']); $rating = $this->rating->getRating($args['id']); } } $container['App\Action\ProdutoAction'] = function ($c) { return new App\Action\Produto($c); };

I’m not quite sure what you’re trying to achieve here, but it should cause performance issues. In essence, we’re talking about 1 level of indirection; given everything else that goes on in a modern web-app, I doubt you’ll notice the cost of this call.

I spend the whole container into the class, that way I do not need to pass the parameter parameter to class I’ll use, then I picked up the container that will use the magic method __get

If that works for you, I’d say ‘go for it’. Like I said above, I really doubt that this would result in a noticeable performance difference.

I don’t think performance will be affected, but you can make a small adjustment:

Replace:

    public function __get($key)
    {
        if ($this->container->has($key)) {
            return $this->container->{$key};
        }
    }

With

    public function __get($key)
    {
        return $this->container->get($key);
    }

Because the slim/container’s magic getter just calls its own get method.

See https://github.com/slimphp/Slim/blob/3.x/Slim/Container.php#L172-L175