1. Go to this page and download the library: Download molajo/ioc 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/ */
molajo / ioc example snippets
use Molajo\IoC\Container;
/**
* Inversion of Control Container
*
* @var object
* @since 1.0.0
*/
protected $iocc;
/**
* Get a service instance
*
* @param string $service
* @param array $options
*
* @results null|object
* @since 1.0
* @throws FrontcontrollerException
*/
public function getService($service, $options = array())
{
return $this->ioc->getService($service, $options);
}
/**
* Replace the existing service instance
*
* @param string $service
* @param object $instance
*
* @results $this
* @since 1.0
* @throws FrontcontrollerException
*/
public function setService($service, $instance = null)
{
$this->ioc->getService($service, $instance);
return $this;
}
/**
* Clone the existing service instance
*
* @param string $service
*
* @results null|object
* @since 1.0
* @throws FrontcontrollerException
*/
public function cloneService($service)
{
return $this->ioc->cloneService($service);
}
/**
* Remove the existing service instance
*
* @param string $service
*
* @results $this
* @since 1.0
*/
public function removeContainerEntries($service)
{
$this->ioc->removeContainerEntries($service);
return $this;
}
/**
* Initialise Application, including invoking Inversion of Control Container and
* Services defined in Services.xml
*
* @return $this
* @since 1.0.0
* @throws FrontcontrollerException
*/
public function initialise()
{
$this->checkPHPMinimum();
set_exception_handler(array($this, 'handleException'));
set_error_handler(array($this, 'handlePHPErrors'), 0);
$connect = $this;
$getService = function ($service, $options = array()) use ($connect) {
return $connect->getService($service, $options);
};
$setService = function ($service, $instance) use ($connect) {
return $connect->setService($service, $instance);
};
$cloneService = function ($service) use ($connect) {
return $connect->cloneService($service);
};
$removeService = function ($service) use ($connect) {
return $connect->removeContainerEntries($service);
};
$services_folder = 'Molajo\\Services';
$this->ioc = new Container($getService, $setService, $cloneService, $removeService, $services_folder);
// Automatically Load These Services
$xml_string = $this->readXMLFile(__DIR__ . '/' . 'Services.xml');
$services = simplexml_load_string($xml_string);
foreach ($services->service as $service) {
$this->getService((string)$service->attributes()->name, array());
}
return;
}
$getService = $this->getService;
$application = $getService('Application');
/** Has cache been activated? */
$cache_service = $application->get('cache_service');
if ((int)$cache_service === 0) {
return $this;
}