Question

When I extend a class that lives in the same directory level it works fine, but when the extending class lives a lateral-sub directory level, it does not. Everything is namespaced. Below is my dir struct and the relevant code.

Dir Struct:

myproj\app\lib\Myproj\
                  |- ...
                  |- Abc\
                      |-  Sublvl1\
                              |- Sublvl1Controller.php
                      |- AbcBaseController.php
                      |- XyzController.php

composer.json:

"psr-0": {
    MyProj\\": "app/lib"
}

The Base Class that I want to extend in various places: \app\lib\MyProj\Abc\AbcBaseController.php:

<?php namespace MyProj\Abc;

class AbcBaseController extends \BaseController 
{
    public function __construct()
    {   dd('inside AbcBaseController');  // testing that the class is being applied when extended...

app\lib\MyProj\Abc\XyzController.php:

<?php namespace MyProj\Abc;
class XyzController extends AbcBaseController // works fine
{

app\lib\MyProj\Abc\Sublvl1\Sublvl1Controller.php:

<?php namespace MyProj\Abc\Sublvl1;

use MyProj\Abc\AbcBaseController;

class Sublvl1Controller extends AbcBaseController   // does not work
{
    ...

The AbcBaseController is not applied when inside the MyProj\Abc\Sublvl1 namespace, even though I import it with the "use" statement. However, the AbcBaseController is applied when inside the MyProj\Abc namespace.

Any insight greatly appreciated.

Était-ce utile?

La solution

It looks like your child class is not calling the parent's constructor:

class Sublvl1Controller extends AbcBaseController   
{

    public function __construct( ) 
    {
        parent::__construct();  // You need this

    //...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top