PHP code example of toolkit / di

1. Go to this page and download the library: Download toolkit/di library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

toolkit / di example snippets


public function set(string $id, mixed $definition, array $opts = []): Container

   $definition = className
    
   $definition = [
      // 1. 仅类名 参数($definition[0])则传入对应构造方法
      'class' => 'className',
      // 2. 类的静态方法,  参数($definition[0])则传入对应方法 className::staticMethod(args...)
      'class' => 'className::staticMethod',
      // 3. 类的动态方法,  参数($definition[0]) 则传入对应方法 (new className)->method(args...)
      'class' => 'className->method',
 
      // 设置参数方式, 没有key
      [
          arg1,arg2,arg3,...
      ]
 
      // 设置属性 , // prop1 prop2 prop3 将会被收集 作为属性
      prop1 => value1,
      prop2 => value2,
      ... ...
      
      // 一些服务设置(别名,是否共享). 将会合并到最后一个参数中
      '_options' => [...] 
   ]

   $definition = new xxClass();

   $definition = function($di){ 
        return object;
   };

  [
   'shared' => (bool), 是否共享,单例
   'locked' => (bool), 是否锁定服务
   'aliases' => (array), 别名
   'init' => (bool), 立即初始化
  ]