php create a new instance of my class without knowing its name

Posted on 2010-01-02
Last Modified: 2013-12-13
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.
Question by:jackjohnson44

    Expert Comment

    This code is used in your class ?

    You can use: $tmpNewInstance = new self;
    LVL 11

    Accepted Solution


    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:
    LVL 107

    Expert Comment

    by:Ray Paseur
    "without knowing my class name?" - What an interesting design. Why do you do that?  

    Author Comment

    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.


    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
            $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

    category would need categorymultiple, item would need itemmultiple, etc

    Does that make sense?
    Is there a better way to do it?
    LVL 107

    Expert Comment

    by:Ray Paseur
    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

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now