Yo lo hago así:
class main{
var $need;
function __construct(){
$this->need = array('class1','class2');
}
}
...
function load_lib($lib){
$this->$lib = new $lib();
foreach($this->$lib->need as $need){
$this->$lib->$need = new $need();
}
}
Se que no es lo mas adecuado ni lo mas correcto pero yo suelo utilizar funciones que recursivamente agregan las librerias que requiero desde un array dentro del constructor, es parte de mi framework y se cargan inmediatamente cuando las llamo y quedan establecidas en la clase principal del sistema incluyendo las incluidas de forma recursiva, o sea, cada clase cargada a su ves hereda todas las demás clases por defecto y puedes llamarlas desde donde te sea mas cómodo.
Mira, dale un vistazo al archivo principal de mi framework:
<?php if($_SERVER['SCRIPT_FILENAME'] == __file__) exit; /* FrameworkDrawCoders : 3.3 / Last modification : 20-Oct-2011 Class version : 1.6 */
/* Execute */
$system->prepare_permalinks();
$system->stripslash_superglobals();
$system->load_plugins('pre');
$system->load_module();
$system->load_theme();
$system->load_plugins('pos');
$system->theme->show_all();
$system->terminate();
/* End execute */
/* Class vars */
var $sidebars;
var $main_menu;
/* System Vars */
var $last_error;
/* Default libs */
var $conf;
var $str;
var $path;
var $headers;
function __construct(){
include(dirname(__file__).'/configuration.php'); $this->conf = new configuration();
$this->conf->apply();
/* Global functions */
$this->load_global_libs();
$this->headers->ctype_html();
}
function load_global_libs(){
/* Make matrix */
foreach($this->conf->load_default_libs as $lib){
include_once($this->conf->lib_dir .$lib.'.php');
$this->$lib = new $lib();
$this->$lib->conf = $this->conf;
}
}
/* Inject objects */
foreach($this->conf->load_default_libs as $lib){
foreach($this->conf->load_default_libs as $inject){
if((is_object($this->$inject)) and
($inject != $lib)){ $this->$lib->$inject = $this->$inject;
}
}
}
}
function load_ret_lib($lib){
return false;
if(!include_once($this->conf->lib_dir .$lib.'.php'))
return false;
$retOB = new $lib;
/* Import default libs */
foreach($this->conf->load_default_libs as $lib_def){
if($lib_def != $lib){
if($this->$lib_def)
$retOB->$lib_def = $this->$lib_def;
else{
include_once($this->conf->lib_dir .$lib_def.'.php');
$retOB->$lib_def = new $lib_def;
}
}
}
/* Configurations */
$retOB->conf = $this->conf;
/* Import libs needs */
foreach($retOB->libs_need as $import){
if($this->$import)
$retOB->$import = $this->$import;
else{
include_once($this->conf->lib_dir .$import.'.php');
$retOB->$import = new $import;
}
}
}
return $retOB;
}
function load_lib($lib){
return false;
return $this->$lib;
if(!include_once($this->conf->lib_dir .$lib.'.php'))
return false;
$this->$lib = new $lib();
/* Import default libs */
foreach($this->conf->load_default_libs as $lib_def){
if((is_object($this->$lib_def)) and
($lib_def != $lib)){ $this->$lib->$lib_def = $this->$lib_def;
}
}
/* Configurations */
$this->$lib->conf = $this->conf;
/* Import libs needs */
foreach($this->$lib->libs_need as $import){
$this->load_lib($import); /* Make lib */
$this->$lib->$import = $this->$import; /* Import lib */
}
}
return $this->$lib;
}
function load_plugins($prefix){
if($plugins = glob($this->path->plugins('local').$prefix.'*.php')){ foreach($plugins as $plugin){
include_once($plugin);
}
}
}
function load_module(){
if($inc = $this->mod_selected()){
$this->conf->module_used_dir = $this->path->modules('relative') .$inc.'/';
$this->conf->module_selected = $inc;
}else{
$this->conf->last_code_status = 404;
$this->conf->module_used_dir = $this->path->modules('relative') .'status/';
$this->conf->module_selected = 'status';
}
include_once($this->path->actual_mod('local') .'index.php'); /* Main file of module */
}
function load_component($file){
if(file_exists($inc = $this->path->actual_mod('local') .'/__layout/'.$file.'.php')){ include_once($inc); /* Actual mod */
}elseif(file_exists($inc = $this->path->modules('local') .$this->conf->main_mod .'/__layout/'.$file.'.php')){ include_once($inc); /* Main mod */
}
return $buff;
}
function load_theme(){
if($this->conf->use_theme){
/* Load components */
$this->theme->buffer_footer = $this->load_component('footer');
$this->load_component('main_menu');
$this->load_component('sidebars');
/* Load theme */
$this->conf->theme_dir = $this->conf->themes_dir .$this->conf->id_theme .'/';
if(file_exists($this->path->theme('local').'index.php')){ include($this->path->theme('local').'index.php');
$this->theme->buffer_module = ''; /* less memory */
}else
$this->theme->buffer_theme = 'Theme not exist.';
}
}
function terminate(){
if($this->sql->handle)
$this->sql->close();
}
}
function mod_selected(){
if(!isset($_GET[$this->conf->var_get])) return $this->conf->main_mod;
if(!$inc = preg_replace('|[^A-Za-z0-9_,-]|i', '', $_GET[$this->conf->var_get])) return false;
if(!file_exists($this->conf->modules_dir .$inc.'/index.php')) return false;
return $inc;
}
function stripslash_superglobals(){
$_SERVER = array_map('stripslashes', $_SERVER); $_COOKIE = array_map('stripslashes', $_COOKIE); $_REQUEST = array_map('stripslashes', $_REQUEST); }
}
function prepare_permalinks(){
if(isset($_SERVER['REDIRECT_URL'])){ /* Mod rewrite -> to php */ if(dirname($_SERVER['SCRIPT_NAME']) != '/') $permalink = str_replace(array(dirname($_SERVER['SCRIPT_NAME']), '.html'), '', $_SERVER['REDIRECT_URL']); // /permalink/x/aaaaa.html else
$permalink = str_replace(array('.html'), '', $_SERVER['REDIRECT_URL']); // /permalink/x/aaaaa.html
if(substr($permalink, 0, 1) == '/') $permalink = substr($permalink, 1); $permalink = explode('/', $permalink);
foreach($permalink as $id => $var){
if($id == 0){
$_GET[$this->conf->var_get] = $var;
}elseif($id % 2 == 0){
$_GET[$prevar] = $var;
}else{
$prevar = $var;
}
}
if($prevar)
}
}
}
Después hago esto:
<?php
$this->load_lib('paginator');
$this->paginator->load_sql('select * from {prefix}users');
$this->paginator->makeHtml();
echo 'Con un total de '.$this->paginator->pages->toInt().' paginas.';
?>
Y si te fijas llamé a la clase paginator que se cargó dentro de la clase principal y a su ves paginator requiere por defecto la clase str que me transforma las variables a objetos para pharsearlas como por ejemplo $this->user->name->toHtml() o $this->str->_get('algo')->toHtml() y son solo recursiones simples:
<?php if($_SERVER['SCRIPT_FILENAME'] == __file__) exit; /*
FrameworkDrawCoders : 3.*
Last modification : 29-4-2011
Class version : 1.3
*/
class user{
/* System Vars */
var $last_error;
var $libs_need;
/* Default libs */
var $conf;
var $str;
var $path;
var $headers;
/* Libs need */
var $sql;
/* Class Vars */
var $is_logged = false;
var $is_loaded = false;
var $user_data = false;
function __construct(){
$this->libs_need = array('sql'); /* Need */ }
function load($arr = false){
$find = '';
$count = 0;
foreach($arr as $var => $val){
$val = new str($val);
$find .= $var.' = "'.$val->toSql().'"';
$count++;
if($count != count($arr)) $find .= ' and ';
}
if(!$this->user_data = $this->sql->fast_select('select * from {prefix}users where '.$find.' limit 1', true))
$this->last_error = array('id' => 1, 'text' => 'Value(s) dont match'); else{
foreach($this->user_data as $var => $val){
$this->$var = new str($val);
}
$this->is_loaded = true;
}
}
return $this;
}
function login($arr_condition = false){
if(!$this->is_loaded){
$this->last_error = array('id' => 13, 'text' => 'Is not loaded'); return $this;
}
if($arr_condition['password'])
$arr_condition['password'] = $this->cryptPass($arr_condition['password']);
foreach($arr_condition as $var => $val){
if($this->$var->val() != $val){
$this->last_error = array('id' => 14, 'text' => 'Values dont match'); return $this;
}
}
} /* else{ Autologin without data } */
/* Update database */
'token' => $this->makeHash(),
));
/* Set the cookie */
'id_user' => $this->id->toInt(),
'password' => $this->password->val(),
'token' => $this->token->val()
)), false, '/');
$this->is_logged = true;
return $this;
}
function loadCookie(){
'id' => (int)$cookie['id_user'],
'password' => $cookie['password'],
'token' => $cookie['token']
));
if($this->is_loaded){
$this->is_logged = true;
$this->update(array('last_login' => time())); }
}else
$this->last_error = array('id' => 3, 'text' => 'No cookie to login'); return $this;
}
function logout(){
if($this->is_loaded and $this->is_logged){
$this->update(array('token' => $this->makeHash())); /* AntiHacking account->cookie */ setcookie($this->conf->cookiename, '', false, '/'); $this->is_logged = false;
foreach($this->user_data as $var => $val){
}
$this->user_data = '';
$this->is_loaded = false;
}else{
$this->last_error = array('id' => 16, 'text' => 'No cookie to login'); }
return $this;
}
function newAccount($arr_data){
/* Set default data */
if($arr_data['password']) $arr_data['password'] = $this->cryptPass($arr_data['password']);
if(!$arr_data['token']) $arr_data['token'] = $this->makeHash();
if(!$arr_data['register_date']) $arr_data['register_date'] = time();
if(!$this->sql->update('users', $arr_data))
$this->last_error = array('id' => 8, 'text' => $this->sql->last_error['text']); return $this;
}
function update($arr_data){
if(!$this->is_loaded){
$this->last_error = array('id' => 9, 'text' => 'No data loaded for update'); return $this;
}
if($arr_data['password'])
$arr_data['password'] = $this->cryptPass($arr_data['password']);
foreach($arr_data as $var => $val){
$this->user_data[$var] = $val;
$this->$var = new str($val);
}
return $this->sql->update('users', $arr_data, 'id = '.$this->id->toInt());
}
function delete(){
if($this->sql->raw('delete from {prefix}users where id = '.$this->id->toInt())){
$this->is_logged = false;
$this->is_loaded = false;
foreach($this->user_data as $var => $val){
}
$this->user_data = false;
}
}else
$this->last_error = array('id' => 11, 'text' => 'Error in delete user. Db error'); }
function makeHash($length = 10){
}
function cryptPass($password){
return sha1($password.sha1("\xa1__".$password)."__\x01"); }
function levelPass($password = false){
if(!$password) return 0;
$level = 0;
if(strlen($password) > 1) $level += 1; if(strlen($password) > 2) $level += 1; if(strlen($password) > 3) $level += 3; if(strlen($password) > 4) $level += 5; if(strlen($password) > 5) $level += 10; if(strlen($password) > 6) $level += 10; if(strlen($password) > 7) $level += 10; if(strlen($password) > 10) $level += 10; if(strlen($password) > 20) $level += 25; return $level;
}
}
Si te fijas en este caso no fue necesario solicitar la clase str en el array porque el sistema ya carga algunas clases por default en el archivo de configuraciones que son str, conf y path.
Si le hago print_r() a la clase se pueden ver las recursiones, si modificas $this->conf es lo mismo que modificar $this->algo->conf
print_r($this->user); exit;
...
[str] => str Object
*RECURSION*
[path] =>
[headers] => headers Object
(
[last_error] =>
[libs_need] =>
[conf] => configuration Object
(
[script_url] =>
http://127.0.0.1/x/index.php...