You can implement a factory to create an object for you. If you are using PHP 5.3.0+, then you will have access to a function called Late Static Binding that will simplify this.
<?php class MyObject { protected $loadSuccess = false; protected function __construct ($foo, $bar, $baz) { // Just a random 50/50 success probability. Put real logic for testing success here $this -> loadSuccess = (mt_rand (0, 99) > 49); } static public function factory ($foo, $bar, $baz) { $objClass = get_called_class (); $obj = new $objClass ($foo, $bar, $baz); if ($obj -> loadSuccess) { return ($obj); } } } class MySubClass extends MyObject { protected function __construct ($foo, $bar, $baz) { parent::__construct ($foo, $bar, $baz); if ($this -> loadSuccess) { echo ('Hello, I\'ma ' . get_called_class () . '<br>'); } } } class MySubSubClass extends MySubClass { protected function __construct ($foo, $bar, $baz) { parent::__construct ($foo, $bar, $baz); if ($this -> loadSuccess) { echo ($foo * $bar * $baz . '<br>'); } } } $o1 = MyObject::factory (1, 2, 3); // Base class $o2 = MySubClass::factory (4, 5, 6); // Child class $o3 = MySubSubClass::factory(7, 8, 9); // Descendent class var_dump ($o1); var_dump ($o2); var_dump ($o3); ?>
If you use a version of PHP prior to 5.3.0, then the situation becomes a little more complicated, since late static binding and get_called_class () are not available. However, PHP 5 does support reflection, and you can use it to emulate late static bindings. Alternatively, you can implement a separate factory class and pass an argument to it that indicates which object you want to initialize.
Another approach is to force your constructor to throw an exception if it fails, and handle it with a catch block. However, this method can get a hairy look if you do it a lot, and should be used only when you usually expect the creation of the object to succeed (in other words, if the creation failure is an exceptional circumstance).
The factory approach has additional advantages, for example, it may contain a cache of objects that you have already created, and if you try to create the same object twice, it will simply give you a link to an already initialized object for creating a new one. This eliminates the memory and processing inherent problems of creating an object, especially if the creation involves a time-consuming task, such as a complex SQL query