1. Go to this page and download the library: Download fanqingxuan/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/ */
fanqingxuan / di example snippets
+HTML
composer
use JsonDi\Di;
class Test
{
}
$di = new Di;
//注入的方式
$di->set('test', 'Test');
$di->set("test2", function () {
return new Test;
});
$di->set("test3", Test::class);
$di->set('test4', new Test);
class UserService
{
protected $userDao;
protected $userType;
public function setUserDao(UserDao $userDao)
{
$this->userDao = $userDao;
}
public function setUserType($userType)
{
$this->userType = $userType;
}
}
$di->setShared(
'db',
function() {
return new MysqlDb();
}
);
$di->set(
'db',
function() {
return new MysqlDb();
},
true
);
class Test
{
}
//register service
$di->set('test','StdClass');
//get service
$test = $di->getService('test');
//change the definition
$test->setDefinition(function() {
return new Test;
});
//resolve the service
$test->resolve();
use JsonDi\Di;
use JsonDi\Di\AbstractInjectionAware;
class Mysql
{
public function select()
{
return "this is select";
}
}
class HomeController extends AbstractInjectionAware
{
public function say()
{
echo $this->container->get('db')->select();
}
}
$di = new Di;
$di->set('db', Mysql::class);
$di->set('home', HomeController::class);
$di->get('home')->say();
use JsonDi\Di\DiInterface;
use JsonDi\Di\ServiceProviderInterface;
class SessionServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di):void
{
$di->set(
'session',
'SessionClass'
);
}
}
$di->register(new SessionServiceProvider());
$di->get('session');
$di->db;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.