Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with extending core class. #1922

Closed
enix-app opened this issue Apr 8, 2019 · 1 comment
Closed

Problem with extending core class. #1922

enix-app opened this issue Apr 8, 2019 · 1 comment

Comments

@enix-app
Copy link
Contributor

enix-app commented Apr 8, 2019

Description/Detail

I want to extend CodeIgniter\View\View class with additional methods.
I'm using view layouts.

<?php

class MyView extends \CodeIgniter\View\View {

    public function asset()
    {
        //....
    }
}

app/Config/Services.php file:

<?php namespace Config;

use CodeIgniter\Config\Services as CoreServices;
use CodeIgniter\Config\BaseConfig;

require_once SYSTEMPATH . 'Config/Services.php';

class class Services extends CoreServices
{
    public static function renderer($viewPath=null, $config=null, bool $getShared=true)
    {
        if ($getShared)
        {
            return static::getSharedInstance('renderer', $viewPath, $config);
        }

        if (is_null($config))
        {
            $config = new \Config\View();
        }

        if (is_null($viewPath))
        {
            $paths = config('Paths');
            $viewPath = $paths->viewDirectory;
        }

        return new \MyView($config, $viewPath, static::locator(true), CI_DEBUG, static::logger(true));
    }
}

In my view file, shared instance always return \CodeIgniter\View\View class, not MyView class.

app/Views/sample.php file:

<?php

// Error: Call to undefined method CodeIgniter\View\View::asset()   
$this->asset();

My solution is simple, only move CodeIgniter\Config\BaseService::getSharedInstance() to Config\Services::getSharedInstance().

Any idea? Thanks.

Version

CodeIgniter 4.0.0-beta-2

Affected module(s)

Config services files.

@lonnieezell
Copy link
Member

Changing the location of that method will only break functionality and not solve anything.

This isn't a bug, though, so should be asked on the forums.

Bigger question is, though - why are you extending Config\Services? That file is there for you to edit. That's it's sole purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants