Skip to content

Bug: Usage of static::methodName in CodeIgniter\Config\Services prevents Service overriding #2376

Closed
@dafriend

Description

@dafriend

The Bug

There are multiple methods in CodeIgniter\Config\Services that make calls to other methods in the class using the form static::someMethod().

For instance, see CodeIgniter\Config\Services::renderer() where the last line in the method is

return new \CodeIgniter\View\View(
            $config, 
            $viewPath, 
            static::locator(true), 
            CI_DEBUG, 
            static::logger(true) //  local scope!!!
);

The problem is that any overriding service defined in APPPATH/Config/Services.php is not in scope so the core class is delivered and not the extended one.

The following services are all called using the static::someMethod() form and so cannot be extended.

  • logger
  • request
  • response
  • locator
  • routes
  • cache
  • renderer

And I might have missed some. There are quite a few CodeIgniter\Config\Services that make static calls to those services so the ramifications would conceivably multiply.

CodeIgniter version
CI 4.0.0-rc.3 at commit 2c04e1c (10/28/19)

Affected module
Services

Reproducing the Problem

Extend Logger

<?php
namespace App\Log;

use Psr\Log\LoggerInterface;
use CodeIgniter\Log\Logger;

class Logger extends Logger implements LoggerInterface
{
    public function myNewMethod($message)
    {
        echo "New Logger Method says: $message";
    }
}

In APPPATH/Config/Services.php

public static function logger(bool $getShared = true)
{
    if ($getShared)
    {
        return static::getSharedInstance('logger');
    }

    return new \App\Log\Logger(new \Config\Logger\Logger());
}

In a controller (of your choice)

$this->logger->myNewMethod('There be Dragons!');

Produces the following error

Call to undefined method CodeIgniter\Log\Logger::myNewMethod()

Context

  • OS: Linux (Debian 9)
  • Web server Apache version 2.4.25
  • PHP version 7.2.17

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions