Makig a dynamic loading/unloading class
Posted: Fri Jun 02, 2006 7:10 pm
i created a loader class within my system. it's not really nessessary but i wanted to fool around since i didn't feel like making a database object right now. it's 'working' i'm just missing a function or two and this would be more fun anyways lmao.
well another reason is because i'm trying to limit the amount of globals within the system and limit them to under 10 hoenstly. yes, the whole system is in classes and i know the hit i'm going to take but the nice and neat code makes me soooooo happy you don't even know.
ok, my loader class as it stands consists of just 1 function and 1 variable. i planned to make it more but this is where i need help.
this is the loader class
you pass it the name of the class and it gets it for you and puts it into the class object variable. sweet
now in the index.php i have this:
this is the class that it's grabbing:
now. this works like a charm. the problem is (so i think) is that php loads everything up right at runtime. and i do not want it to add all the files to memory that doesn't need to be there. i'm trying to be conservative here with my code (C background
:D) and i want to tell it when to load and unload files/classes and whatever.
so, if i have both those includes it'll load them up and it's easy for it to know where to get the class names. which is awesome but also not so much at the same time IMO. i want to have functions in there that are specific for certain things. such as load the database object, or load a PHP tool or load the user information. you understand? i planned to work the whole system like this so i could also limit my include statements as well.
any ideas?
example load function
thanx for your help. i'm not sure if this should be in a more appropriate area but i don't think it's that complex of an idea. well, for you guys 
well another reason is because i'm trying to limit the amount of globals within the system and limit them to under 10 hoenstly. yes, the whole system is in classes and i know the hit i'm going to take but the nice and neat code makes me soooooo happy you don't even know.
ok, my loader class as it stands consists of just 1 function and 1 variable. i planned to make it more but this is where i need help.
this is the loader class
Code: Select all
<?php
//[b]loader.php[/b]
class Loader
{
var $cached_vars = array();
function load($name)
{
global $load;
$this->object = new $name();
}
}
?>now in the index.php i have this:
Code: Select all
//[b]index.php[/b]
include 'class.php';
include 'loader.php';
$load;
$CI;
$loader = new Loader();
$loader->load("everything");
$loader->object->thing();Code: Select all
//[b]class.php[/b]
class everything
{
function thing() { echo 'thing'; }
}now. this works like a charm. the problem is (so i think) is that php loads everything up right at runtime. and i do not want it to add all the files to memory that doesn't need to be there. i'm trying to be conservative here with my code (C background
so, if i have both those includes it'll load them up and it's easy for it to know where to get the class names. which is awesome but also not so much at the same time IMO. i want to have functions in there that are specific for certain things. such as load the database object, or load a PHP tool or load the user information. you understand? i planned to work the whole system like this so i could also limit my include statements as well.
any ideas?
example load function
Code: Select all
function loadDatabaseObject()
{
// check which type of DB we are running. from the config file
// goto the folder where the DB files are stored
// inlucde those files
}
function unloadDatabaseObject()
{
// unload whatever object we have in the class array variable.
}