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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?