0

I got two index.php and both make use of a bootstrap.php. The bootstrap file is setting up a DI-Container and I need in both index files access to this DI-Container.

First I was thinking about using a simple return in bootstrap.php:

bootstrap.php

<?php
require __DIR__ . '/vendor/autoload.php';
$container = new League\Container\Container;
// add some services
return $container;

index.php

<?php
$container = require __DIR__ . '/bootstrap.php';
$container->get('application')->run();

I read somewhere that using return statements like this are a bad habit. So I'm wondering how to make the container in the index.php accessible in an easy and proper way?

Anders
  • 8,307
  • 9
  • 56
  • 88
nipec
  • 67
  • 4
  • If you really need the >>same<< Container Object you could try a singleton pattern, here is a good answer for it: http://stackoverflow.com/a/203359/5297359 – swidmann Oct 06 '15 at 07:27
  • One index.php is the starting point for the frontend and the other one for the backend of my application. The two index files are not executed in the same request - so no, I don't need the same container object. I am just wondering how to access the container instance. I didn't want to create a Singleton or Registry cause I would use them only for this single purpose. – nipec Oct 06 '15 at 07:45
  • OK than just drop the return in `bootstrap.php`, just use a simple `require`without assigning it to `$container`, then you can use the `$container` variable from the bootstrap without doing anything else – swidmann Oct 06 '15 at 07:49

1 Answers1

0

There is no need for a return, if you include the file you already have the access to the variable $container

bootstrap.php

<?php
require __DIR__ . '/vendor/autoload.php';
$container = new League\Container\Container;
// add some services

index.php

<?php
require __DIR__ . '/bootstrap.php';
$container->get('application')->run();

UPDATED (following the comments):

bootstrap.php

<?php
require __DIR__ . '/vendor/autoload.php';
// add some services
return new League\Container\Container;

index.php

<?php
$container = require __DIR__ . '/bootstrap.php';
$container->get('application')->run();

ANOTHER EXAMPLE:

If you need to add your services on the Container object before return, you can use a static helper class (just for example) if you want to avoid a global variable:

class Context {
    private static $container = null;

    public static function getContainer() {
        return self::$container;
    }
    /* maybe you want to use some type hinting for the variable $containerObject */
    public static function setContainer( $containerObject ) {
        self::$container = $containerObject;
    }
}

bootstrap.php

<?php
require __DIR__ . '/vendor/autoload.php';
// require the Context class, or better get it with your autoloader
Context::setContainer( new League\Container\Container );
// add some services
Context::getContainer()->addMyService();
Context::getContainer()->addAnotherService();

//if you want to, you can return just the container, but you have it in your Context class, so you don't need to
//return Context::getContainer();

index.php

<?php
require __DIR__ . '/bootstrap.php';
Context::getContainer()->get('application')->run();
swidmann
  • 2,787
  • 1
  • 18
  • 32
  • I'd call this even worse, since you're defining a hardcoded global variable name here. `return` is much more preferable, since it leaves the choice of variable name up to the caller. – deceze Oct 06 '15 at 08:05
  • Yes you're right with a return I can have my own new variable name, but the global `$container` variable is still existing even if there is a return statement. So you would also need to drop the `$container` in the bootstrap.php and just `return new League.......` – swidmann Oct 06 '15 at 08:12
  • Indeed, that'd be the much better solution. – deceze Oct 06 '15 at 08:16