Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:14:38+00:00 2026-06-03T23:14:38+00:00

Basically my setup is that I have a lot of objects that I want

  • 0

Basically my setup is that I have a lot of objects that I want to create. What type of object it will become is dependent on one variable, which is the type. So originally I would have to do many if statements, so to shorten it I created an array, but I am running in to the problem of being able to create the actual object through the array.

Here is what I had originally:

if($object->type = 'text')
{
    $object_new = new Text();
} elseif($object->type = 'image') {
    $object_new = new Image();
} ....

But what I wanted to do is:

$all_objects = array('text'=> new Text(), 'image' => new Image(), ...);
$object_new = $all_objects($object->type);

Which would shorten my code by a lot as well as make it more efficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:14:39+00:00Added an answer on June 3, 2026 at 11:14 pm

    If there is no further dependencies to these object, you can simply do

    $className = $object->type;
    $instance = new $className;
    

    If you need more sophisticated creation logic, you can put the creation code in Lambdas and call those then, e.g.

    $allTypes = array(
        'text' => function($string) { return new Text($string); },
        'Image' => function($path) { return new Image($path); }
    );
    $instance = call_user_func_array($allTypes[$object->type], array($argument1));
    

    Or you create a Factory object and put a switch/case into it:

    class Factory
    {
        public function create($type)
        {
            switch ($type)
            {
                case 'text':
                    return new Text;
                case 'image':
                    return Image($this->create('foo'));
                case 'foo':
                    return new Foo;
                default:
                    throw new Exception('No clue what to create');
            }
        }
    }
    

    Also checkout Is there a call_user_func() equivalent to create a new class instance?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have some variables that I don't want to preinitialize: originalTime = None
Basically I have a link at the bottom of a lot of content that
My question basically is that I have lot of routes set up for CRUD
Ok..... basically what I'm trying to do is setup something that will handle all
Basically I need to setup my ASMX file so that when I pull it
Basically I want to setup a replication server for mysql datbase. I am completely
basically i have (state, state code) pairs, that are subsets of country [USA] ->
I have a list of ID numbers that I pull from a session variable
Let's say I'm basically inheriting a live site that has a lot of errors
I have cron jobs setup that runs a few PHP scripts often. The issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.