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 978073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:00:21+00:00 2026-05-16T04:00:21+00:00

Well, I have a problem (ok, no real problem, but I wanna try out

  • 0

Well,

I have a problem (ok, no real problem, but I wanna try out something new) with creating objects. Actually I have some orders, which contains a list of orderitems.

These orderitems are used and so spreaded in the whole application, and I need a way to create them. The main problem is, I want to be able to create these objects in many different ways.

Actually I do this in the class constructor and check if the argument which is given.
(I’m using php, so there is no overloading support from the language as you surely know :))

A simple and quick Example

class foo {
    protected $_data=null;
    public function __contruct($bar){
        if (is_array($bar)){
            $this->_data=$bar;
        }
        else {
            $dataFromDb=getDataFromDatabase
            $this->_data=$dataFromDb;
        }
    }
}

Anyway, if I want to create my object by giving another type of parameter, lets say a xml-document encapsulated in a string I need to put all this stuff in my constructor.
If the process for creating an object is more complicated, I eventually need to create a seperate method for each type, I want to initiate. But this method is only called when this special type is created. (I think you got the problem :))

Another problem comes to mind, if I need more parameters in the constructor to create a concrete object, I have modify all my code, cause the contructor changed. (Ok, I can give him more and more parameters and work with default values, but that is not what I really want).

So my Question is, which pattern fits this problem to solve my creation of a concrete object. I thought about creating a factory for each way I want to create the concrete object. But I’m not sure if this is a common solution to solve such a problem.

  • 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-05-16T04:00:21+00:00Added an answer on May 16, 2026 at 4:00 am

    IF its only the signature of the constructor changing i would do it like so (a la the Zend Framework universal constructor):

    class foo {
      // params
    
      public function __construct($options = null)
      {
    
        if(null !== $options)
        {
           $this->setOptions($options);
        }
      }
    
      public function setOptions(array $options){
    
        foreach ($options as $name => $value){
          $method = 'set' . $name;
          if(method_exists($this, $method)
          {
             $this->$method($value);
          }
       }
    
       return $this;
      }
    }
    

    And this essntially means all your constructor parameters are array elements with named keys, and anything you want used in this array during initialization you create a setter for and then its automatically called. The down side is the lack of effective hinting in IDEs.

    On the otherhand if you want to have specific constructors then i might go with a factory but still use much the same approach:

    class foo {
    
      public static function create($class, $options)
      {
         if(class_exists($class))
         {
            $obj = new $class($options);
         }
      }
    }
    

    Of course you could alternatively use PHP’s reflection to determine how to call the constructor instead of just injecting an arbitrary array argument.

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

Sidebar

Related Questions

well i have most probably an extremly stupid problem but could not figure it
Well, code speaks more (I have hard-coded some things, to isolate the problem and
I have a problem with debugging sessions. My program executes very well in a
Well here's my problem I have three tables; regions, countries, states. Countries can be
I have the following problem that the standard library doesn't solve well, and I'm
Well, I have a really small problem. How can I access an ASP.NET Drop
Well, this is the exact opposite problem that I normally have with Javascript synchronicity
I have been trying to solve this problem for hours (searched here as well
My title is probably not really describing the problem real well. I do not
well i have this messages table with sample values like these: msg_id recipient_id read

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.