title: OOP (php) instantiating class within another class
body: <p>Not sure whether I picked the proper title. But anyway.</p>
  
  <p>I've got to support and to develop already existing project. </p>
  
  <p>It's built on OOP. </p>
  
  <p>I've got such models as Order, Product.  They are used very often into code. </p>
  
  <p>Every model is bound to a corresponding table. </p>
  
  <p>Client wants to integrate a new kind of customers into system. 
  This kind of customers has different data associated with them and order completely different kinds of Products. So Order will look different.</p>
  
  <p>What I decided is to not to mix the old kind of Order and the newly kind of it.  At first I put them into separated tables in order not to break the working system and created different classes for them. </p>
  
  <p>Now I have model Order.php and OrderNew.php, Product.php and ProductNew.php and so on. </p>
  
  <p>I've global settings object where property of it holds what type of class I need to instantiate. </p>
  
  <p>Now I've messed a lot of code with: </p>
  
  <pre><code>if ($global-&gt;object-&gt;isNewKindOfCustomer())  {
     $product = new ProductNew;   
  } else {
     $product  = new Product;
  }
  </code></pre>
  
  <p>But doing so in many places I got strong feeling that I do something very VERY wrong.</p>
  
  <p>So my first idea was that it should be decided by class Product what kidn of product should be instantiated. </p>
  
  <p>It would be perfect if I  could go to the old Product class and do in its constructor and do somethign like: </p>
  
  <pre><code>Class Product {
    __construct() {
      if ($global-&gt;object-&gt;isNewKindOfCustomer())  {
        $product = new ProductNew;   
      } else {
         $product  = new Product();
      }
    }
   }
  </code></pre>
  
  <p>Additionally I would inherit ProductNew from Product and redefine all methods that needs to be changed. Something like:  </p>
  
  <pre><code>Class ProductNew extends Product {
  
    public methodsDefinedInProductButInNeedToBeChangedForProductNew (){
    }
  }
  </code></pre>
  
  <p>But the problem is that I didn't manage to find a sane way to do it in PHP and it still smells not so good. But much better than the first approach (at least for me).</p>
  
  <p>The third idea is that I will create now just ProductNew class but also ProductOld class (what doesn't exist but what I got per se). </p>
  
  <p>I will move all code from current Product class to ProductOld.
  So my current Product class will have no methods. It will get empty.
  Additionally ProductNew will inherit from ProductOld. </p>
  
  <p>Having such scheme I won't touch code of whole system at all. 
  Everywhere  in the code the assignemnts will have the following look as </p>
  
  <pre><code>$product  = new Product
  </code></pre>
  
  <p>and inside I will need somehow to manager what  object will be created in  Product constructor. </p>
  
  <p>The fourth idea is that I will create a generic class at first and name it as e.g. ProductGeneral  . It will have mehtods related to both ProductNew and ProductOld classes.   They both will extend ProductGeneral. And inside Product only the proper class will be instantiated depending on the role of  current user. Although I'm not sure whether ProductGeneral is so necessary....</p>
  
  <p>Still no idea how I can replace this in Product class. I remember it was called something like dynamic binding in C++.</p>
  
  <p>Are there any better solutions for tackling this problem? Am I missing something?  </p>
  
  <p>Sorry for too long text I did try to make it as short as possible.</p>