I’m trying to setup some classes to work with, but I’m not really sure how to accomplish to following.
Let’s say I have a class “foo” that I’m working in, and I want to use functions from another class called “loader”. The purpose of this loader class is to load various other classes that I can then use within the “foo” class.
Example:
class foo {
function foo() {
$this->load->model();
}
}
For this to work I would first initiate the class “load”:
class foo {
function foo() {
$this->load = new loader;
$this->load->model('some_model');
}
}
Now I can use the functions from within the loader class. The next thing I want is for the loaded model to be accessible from within the class foo. Example:
class foo {
function foo() {
$this->load = new loader;
$this->load->model('some_model');
$this->some_model->function_from_this_model();
}
}
And this is where I get lost, because I’m not sure how to stay within the scope of the “foo” class. At any given time I’d like to be able to use $this->load->model('some_model') to load a new model, that becomes accessible through $this->some_model. Or $this->load->something_else('some_name') which becomes available through $this->some_name.
Here’s a loader class example:
class loader {
function model($model_name) {
require('models/'.$model_name.'.php');
$model = new $model_name;
// and what to do now, to get it back to the $this var from class foo?
}
}
But all of this happens only within the scope of the loader class.
Any ideas on how to accomplish this?
Edit:
I know I can assign it directly to a variable, like $this->model_name = $this->load->model('model_name'), but that’s what I’m trying to avoid. I’d like to be able to use this “load” subclass to be able to work within the scope of the class “foo”.
You can use a Singleton, which will allow you to initialize your class objects from another class.
Example: