donglv9116 2015-10-06 07:24
浏览 34
已采纳

使用包含文件中的容器对象

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?

  • 写回答

1条回答 默认 最新

  • dongxiezhi0590 2015-10-06 07:53
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件