jackjohnson44
asked on
php create a new instance of my class without knowing its name
Hi,
I have a class that returns an array of instances of itself.
so my class is item, and I have a function called getallitems, which gives me all items in the database. To do this, I create an array of itself and push instances on the end.
I have to create a new instance on each loop. Is there a way to do this without knowing my class name?
foreach ($multipleresultsfromdb as $item_single)
{
$tmpNewInstance = new CategoryClass;
$arrayToReturn[] = $tmpNewInstance->setfromdb row($item_ single);
}
return $arrayToReturn;
I want to do this without knowing that CategoryClass is the class name.
I have a class that returns an array of instances of itself.
so my class is item, and I have a function called getallitems, which gives me all items in the database. To do this, I create an array of itself and push instances on the end.
I have to create a new instance on each loop. Is there a way to do this without knowing my class name?
foreach ($multipleresultsfromdb as $item_single)
{
$tmpNewInstance = new CategoryClass;
$arrayToReturn[] = $tmpNewInstance->setfromdb
}
return $arrayToReturn;
I want to do this without knowing that CategoryClass is the class name.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"without knowing my class name?" - What an interesting design. Why do you do that?
ASKER
Thanks guys, this is what I was able to get working
$tmpClassName = get_class($this);
$tmpNewInstanceOfThisClass = new $tmpClassName;
Ray, I am doing this because I am using the codeigniter mvc framework. So I have a model called category (which corresponds to a db table category). I have a function initFromDB($id) which gets the values from the db and sets its own properties to these values and doesn't have to return anything.
So:
$mymodel->initFromDB($id)
print $mymodel->id
print $mymodel->name
This works well.
When I try to get more than one instance of the class (get all categories) I return an array of this type of object, even though the function that creates the array is in the object itself.
This is how I call get all
$allitems = $this->category_model->get All();
$data['item_multiple'] = $allitems;
This is how I call get one
$this->category_model->get ($id);
$data['category_single'] = $this->category_model;
If I don't create a new instance of the class in itself, I would have to make another class that contains an array of this class. I feel that this would get bloated since I would need a collection for each object
so
category would need categorymultiple, item would need itemmultiple, etc
Does that make sense?
Is there a better way to do it?
$tmpClassName = get_class($this);
$tmpNewInstanceOfThisClass
Ray, I am doing this because I am using the codeigniter mvc framework. So I have a model called category (which corresponds to a db table category). I have a function initFromDB($id) which gets the values from the db and sets its own properties to these values and doesn't have to return anything.
So:
$mymodel->initFromDB($id)
print $mymodel->id
print $mymodel->name
This works well.
When I try to get more than one instance of the class (get all categories) I return an array of this type of object, even though the function that creates the array is in the object itself.
This is how I call get all
$allitems = $this->category_model->get
$data['item_multiple'] = $allitems;
This is how I call get one
$this->category_model->get
$data['category_single'] = $this->category_model;
If I don't create a new instance of the class in itself, I would have to make another class that contains an array of this class. I feel that this would get bloated since I would need a collection for each object
so
category would need categorymultiple, item would need itemmultiple, etc
Does that make sense?
Is there a better way to do it?
OK, I understand what you want to do. I just do not know very much about CodeIgniter - that is still PHP4, if I recall correctly, and the OOP in PHP4 is not the best. Sorry - I will have to leave this to others, but best of luck with it. ~Ray
You can use: $tmpNewInstance = new self;