1

I am using League Container package as DI in my project.

class App extends Container {};

above code working fine.but problem trigger whenever in use constructor in my app class. class App extends Container

{  
   public __construct(){};
}

it shows like- Fatal error: Call to a member function add() on null

i want to know what does it causes in oop prgramming

2 Answers2

2

There is a couple of reasons it wouldn't work:

  • public __construct(){}; is not the correct way to define a class construct.

If that's just a typo the actual error you would be getting is:

Fatal error: Call to a member function getDefinition() on a non-object in ...

This is because if you extend a class which has a construct, then you must call the parent construct or it will discard it. (which it does).

So putting them 2 points together here is a working example:

<?php
// require 'vendor/autoload.php';

use League\Container\Container;

class App extends Container {
    public function __construct() {
       parent::__construct();
    }
}

class Foo {

}

$container = new App;

// add foo to the container
$container->add('foo', 'Foo');

// retrieve foo from the container
$service = $container->get('foo');

var_dump($service instanceof Foo); // true
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

You are missing "function" after "public".

jhilgeman
  • 1,543
  • 10
  • 27