Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a object "evolve" into an object of one of it's child classes?
    text
    copied!<p>Assume we have a parent class and a child class. The parent class checks some parameters and decides that the child class would be better suited to handle the given request - is there a way to recreate the existing (parent)-object as an object of the child class from whithin the parent object?</p> <p>Example of a parent class:</p> <pre><code>/** * Simple car */ class car { /** * The constructor */ public function __construct() { // (...) } /** * Add a sunroof to the car */ protected function addSunRoof() { // (...) } /** * Parse a config string and configure the car accordingly * @param string $config */ public function configure($config) { $options = explode(";", $config); // Do something for every option in $config string foreach ($options as $o) { switch ($o) { case "sunroof" : $this-&gt;addSunRoof(); break; case "4x4" : // 'car' does not have support for '4x4', but the child class 'suv' does // -&gt; This 'car' object should therefore 'evolve' and become a 'suv' object - how? case "foo" : // (...) } // switch } // foreach } // configure } // car </code></pre> <p>Example of the child class:</p> <pre><code>/** * SUV car */ class suv extends car { /** * Add 4x4 drive */ protected function add4x4() { // (...) } // add4x4 } // suv </code></pre> <p>Now, the most obvious way to get a <code>suv</code> object would be to create it directly from the beginning:</p> <pre><code>$car = new suv(); $car-&gt;configure($config); </code></pre> <p>The challenge is that I don't know whether a <code>car</code> or a <code>suv</code> object is required when creating the object; I don't know what options the <code>$config</code> string contains until it gets parsed in the <code>$car-&gt;configure()</code> method (The string can come from anywhere, e.g. user input).</p> <p>A simple workaround would be to move the <code>configure</code> method out of <code>car</code> to analyse the string before creating any object - however, it logically belongs to the <code>car</code> object, so I would really like to keep it there :)</p> <p>Is there a way to solve this puzzle? If not, what would you suggest as the "cleanest" workaround?</p> <p>Thanks in advance!</p> <p><strong>Edit:</strong></p> <p>As it was pointed out, my question is very similar to this one: <a href="https://stackoverflow.com/questions/4080217/cast-the-current-object-this-to-a-descendent-class">Cast the current object ($this) to a descendent class</a> The answers there are saying that it's technically possible, but should not be done. However, a convincing alternative has not been suggested yet (It has been suggested to use factories, but I feel this would only solve a part of the problem).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload