代码之家  ›  专栏  ›  技术社区  ›  Imnotapotato

如何获取PHPDI容器?

  •  -1
  • Imnotapotato  · 技术社区  · 5 年前

    PHP DI ? 这是我迄今为止尝试过的变体之一。

    Settings.php

    <?php 
    use MyApp\Core\Database;
    use MyApp\Models\SystemUser;
    
    return [
        'Database'      => new Database(), 
        'SystemUser'    => new SystemUser()
    ];
    

    $containerBuilder   = new \DI\ContainerBuilder(); 
    $containerBuilder->addDefinitions('Settings.php');
    $container          = $containerBuilder->build();
    

    SystemUserDetails.php

    <?php 
    namespace MyApp\Models\SystemUser;
    
    use MyApp\Core\Database;
    use MyApp\Core\Config;
    use MyApp\Helpers\Session;
    
    
    /**
     *
     *  System User Details Class
     *
     */
    class SystemUserDetails 
    {
    
    /*=================================
    =            Variables            =
    =================================*/
    
        private $db;
    
    
    /*===============================
    =            Methods            =
    ================================*/
    
        /**
         *
         *  Construct
         *
         */
        public function __construct(Database $db)
        {
            # Get database instance
            // $this->db           = Database::getInstance();
            $this->db           = $db;
        }
    
    
        /**
    

    函数MyApp\Models\SystemUser\SystemUserDetails的参数太少::u construct(),第54行的/www/MyApp/Models/SystemUser.php中传递了0,预期正好是1 文件:/www/myapp/models/SystemUser/SystemUserDetails.php

    跟踪:

    1. 目前,我的主要 index.php 文件扩展名 init.php

    2. 然后我打电话给 App MyApp/Contollers/Login.php .

      1. 这个 user_login

    系统用户类:

    namespace MyApp\Models;
    
    
    class SystemUser
    {
    
        public $id;
    
        # @obj SystemUser profile information (fullname, email, last_login, profile picture, etc')
        protected $systemUserDetatils;
    
    
        public function __construct($systemUserId = NULL)
        {
            # Create systemUserDedatils obj
            $this->systemUserDetatils   = new \MyApp\Models\SystemUser\SystemUserDetails();
    
            # If system_user passed
            if ( $systemUserId ) {
    
                # Set system user ID
                $this->id                   = $systemUserId;
    
                # Get SysUser data
                $this->systemUserDetatils->get($this);
    
            } else {
    
                # Check for sysUser id in the session:
                $systemUserId                   = $this->systemUserDetatils->getUserFromSession();
    
                # Get user data from session 
                if ( $systemUserId ) {
    
                    # Set system user ID
                    $this->id                   = $systemUserId;
    
                    # Get SysUser data
                    $this->systemUserDetatils->get($this);
                }
            }
        }
    }
    
    1 回复  |  直到 5 年前
        1
  •  5
  •   yivi SilverLink    5 年前

    在你的 SystemUser

    $this->systemUserDetatils   = new \MyApp\Models\SystemUser\SystemUserDetails();
    

    的构造函数 SystemUserDetails 需要一个 Database

    打电话 new 直接地 您没有使用PHP-DI . 通过这样做,你就是 躲藏 依赖性,如果您想使用依赖性注入系统,这正是您想要避免的。

    如果 系统用户 取决于 (“需要”) ,依赖项应该是显式的(例如,在其构造函数中声明)。

    对于这样的系统。你在问题中显示的定义文件不符合要求 the best practices recommended by PHP-DI .

    你的设计远非完美,我也不确定你的最终目标,但如果你做了这样的事情,它可能会起作用:

    <?php
    // src/Database.php
    
    class Database {
        public function getDb() : string {
            return 'veryDb';
        }
    }
    
    <?php
    // src/SystemUserDetails.php
    
    class SystemUserDetails {
    
        protected $db;
    
        public function __construct(Database $db)
        {
            $this->db           = $db;
        }
    
        public function getDetails() {
           return "Got details using " . $this->db->getDb() . '.';
        }
    }
    
    <?php
    // src/SystemUser.php
    class SystemUser {
    
        protected $details;
    
        public function __construct(SystemUserDetails $details, $userId=null) {
    
            $this->details = $details;
        }
    
        public function getUser() {
           return "Found User. " .$this->details->getDetails();
        }
    }
    
    <?php
    //init.php
    require_once('vendor/autoload.php');
    
    // build the container. notice I do not use a definition file.
    $containerBuilder   = new \DI\ContainerBuilder();
    $container          = $containerBuilder->build();
    
    // get SystemUser instance from the container.
    $userInstance = $container->get('SystemUser');
    
    echo $userInstance->getUser(), "\n";
    
    

    其结果是:

    Found User. Got details using veryDb.