[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1192
  • Last Modified:

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->setfromdbrow($item_single);
        }
        return $arrayToReturn;

I want to do this without knowing that CategoryClass is the class name.
0
jackjohnson44
Asked:
jackjohnson44
1 Solution
 
eelcolCommented:
This code is used in your class ?

You can use: $tmpNewInstance = new self;
0
 
VanHackmanCommented:

You can use the getClass() function to get it.

$class      = self::getClass();
$instance = new $class();


$class      = getClass($Object);
$instance = new $class();


$class      = getClass($this);
$instance = new $class();

More info: http://php.net/manual/en/function.get-class.php
0
 
Ray PaseurCommented:
"without knowing my class name?" - What an interesting design. Why do you do that?  
0
 
jackjohnson44Author Commented:
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->getAll();
        $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?
0
 
Ray PaseurCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now