Jump to content

DI php FRAMEWORK


martabau

Recommended Posts

hi all i have few experience with php i'd need some help,

i've been playing with codeigniter, if i'm not wrong codeigniter approaches DI(depency injection) by loading all resources into a global super object which is bound to the ($this) inside controllers and models, inside libraries the object is fetched using the &get_instance() method.

i'm trying to start a very lightweight php framework using some classes from CI, i'm using a registry pattern like this:

class Registry {

/**
 * Array of objects
 */
private $objects;
 */
private $settings;

    public function __construct() {
        
    }
        
   	// --------------------------------------------------------------------
    
    /**
     * Create a new object and store it in the registry
     * @param String $object the object file prefix
     * @param String $key pair for the object
     * @return void
     */

    public function iniObject($class, $key)
    {
      
        
    	$this->objects[ $key ] = new $class( $this );
    }
        
   	// --------------------------------------------------------------------
    
    /**
     * Get an object from the registries store
     * @param String $key the objects array key
     * @return Object
     */
    public function getObject( $key )
    {
    	return $this->objects[ $key ];
    }
        
   	// --------------------------------------------------------------------
    
    /**
     * Store Setting
     * @param String $setting the setting data
     * @param String $key the key pair for the settings array
     * @return void
     */
    public function storeSetting( $setting, $key )
    {
    	$this->settings[ $key ] = $setting;
    }
    
   	// --------------------------------------------------------------------
    
    /**
     * Get a setting from the registries store
     * @param String $key the settings array key
     * @return String the setting data
     */
    public function getSetting( $key )
    {
    	return $this->settings[ $key ];
    }

}

 

i start my bootstrap.php with

 

$registry = new Registry();
/*
* ------------------------------------------------------
*  Instantiate some classes
* ------------------------------------------------------
*/
$registry->iniObject( 'Utf8', 'utf8');
$registry->iniObject( 'Uri', 'uri');

 

i've modified some classes and i pass the registry to the constructor(DI) of CI classes i want to use,

for example:

 

class URI {
private $registry;
private $config;

public function __construct(Registry $registry)
{
    $this->registry = $registry;

        $this->registry->write_log('debug', "URI Class Initialized");
}


// --------------------------------------------------------------------

/**
 * Get the URI String
 *
 * @access	public
 * @return	string
 */
public function _fetch_uri_string()
{
	if (strtoupper($this->registry->getObject('config')->item('uri_protocol')) == 'AUTO')
	{

.

.

.

etc,etc,

 

instead of global CI object i pass to every class i want to use my  registry class, optional i can use a setter injection in a class method,

in every class i've modified the code to use the registry not the global CI object to use methods from any class into another class

using constructor dependency injection,

it works well, i use output, buffering, models, controllers, views like CI does

but using my registry and passing the registry to the constructor of my classes,

my base controller:

 

Class Controller 
{

/*
* @registry object
*/
    protected $registry;

    function __construct(Registry $registry) 
    {
 $this->registry = $registry;
    }

/**
* @all controllers must contain an index method
*/
    public function index() {}
}

 

then i extend any new controller for example and i can load any view:

 

class Catalog extends Controller  {

public function index()
{

        $this->registry->getObject('load')->view('catalog');

}
}

 

less code and more clear for me,

can any php expert tell me if this approach is correct or is better to use CI as is with global static super object and extending base controller,

which tools i can use to know what is better when trying to do php software i mean singleton patterns, registry pattern, etc always

getting MVC(model,view,controller) like the final result

thanks a lot

 

Link to comment
https://forums.phpfreaks.com/topic/245281-di-php-framework/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.