PHP code example of metrophp / metrodi

1. Go to this page and download the library: Download metrophp/metrodi 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/ */

    

metrophp / metrodi example snippets


	$cont = Metrodi_Container::getContainer();
	$cont->set('flaga', true);
	$cont->didef('logService', 'path/to/log.php', 'arg1', 'arg2', 'arg3');
	$log = $cont->make('logService');

	_set('flaga', true);
	_didef('logService', 'path/to/log.php', 'arg1', 'arg2', 'arg3');
	$log = _make('logService');

	$cart = _make('shoppingCartService');
	echo( get_class($cart) );  //  Metrodi_Proto

	_didef('shoppingCartService', 'path/to/my/cart.php');
	$cart = _make('shoppingCartService');
	echo( get_class($cart) );  //  Path_To_My_Cart

	_didef('request',  '\Top\Quality\Request');
	_didef('response', '\Top\Quality\Response');

	class MyController {

		public function __construct($request, $response) {
			echo get_class($request);  // 'Top\Quality\Request';
		}
	}

	namespace org\my\cart\service\abstract\concrete\interface;
	class Cart {
		public function __construct($idUser, $listItems, $timestamp=NULL) {
			$this->idUser    = $idUser;
			$this->listItems = $listItems;
			$this->timestamp = $timestamp;
		}
	}

	_didef('shoppingCartService', '\org\my\cart\service\abstract\concrete\interface\Cart', 'A', 'B');
	$cart1 = _make('shoppingCartService');
	echo $cart1->idUser;     // 'A'
	echo $cart1->listItems;  // 'B'
	echo $cart1->timestamp;  // null

	$cart2 = _make('shoppingCartService', 'C', 'D', time());
	echo $cart2->idUser;     // 'C'
	echo $cart2->listItems;  // 'D'
	echo $cart2->timestamp;  // 1234567890  (YMMV)

   _didef('singletonService', '\ns\locator\class', 'arg1', 'arg2');

   //later
   $ss1 = _make('singletonService');

   //same reference
   $ss2 = _make('singletonService');

   _didef('user', '\ns\locator\class');

   //later
   $u1 = _make('user', 100);

   //new object
   $u2 = _make('user', 200);

   _didef('log', '\ns\locator\class', '/tmp/out.log');

   //later
   $mainLog    = _make('log');
   $specialLog = _make('log', '/var/log/special.log');


   _didef('user', '\ns\user');

   //later
   $user1 = _make('user');
   $user2 = _makeNew('user'); //different instance

    _didef('connection', function($c) {
       return new \Ns\Connection($c['username'], $c['password']);
    });

   $c = _make('connection', array('bob', 'secret');

    _didef('connection', function($c) {
        return new \Ns\Connection($c['username'], $c['password']);
    });

    $c  = _make('connection', array('bob', 'secret');
    $c2 = _makeNew('connection', array('alice', 'secret2');

    class NullMailer {
        public function send() {
            print "Sending...\n";
        }
    }

    class Controller {
        public $emailService;
    }

    _didef('controller', 'Controller');
    _didef('emailService', new NullMailer());

    $c  = _make('controller');
    $c->emailService->send();