You could make private the normal constructor (so it can't be used from outside the object, like you would if you were making a Singleton ) and create a Factory Method class MyClass { private function __construct($input) { // do normal stuff here } public static function factory($input = null) { if (empty($input)){ return null; } else { return new MyClass($input); } } } Then you would instantiate a class like this: $myClass = MyClass::factory($theInput) (EDIT: now assuming you're only trying to support PHP5).
You could make private the normal constructor (so it can't be used from outside the object, like you would if you were making a Singleton) and create a Factory Method. Class MyClass { private function __construct($input) { // do normal stuff here } public static function factory($input = null) { if (empty($input)){ return null; } else { return new MyClass($input); } } } Then you would instantiate a class like this: $myClass = MyClass::factory($theInput); (EDIT: now assuming you're only trying to support PHP5).
1 Good additional explanation. – jheddings Oct 29 '09 at 23:08 Up-votes are appreciated :) – philfreo Oct 29 '09 at 23:15 would I still need the __construct? – Daniel Oct 29 '09 at 23:33 I think you have this the wrong way around.
The constructor should be called __construct(), unless you need to support PHP4. __construct is newer. – Tom Haigh Oct 29 '09 at 23:38 (I think that's what he meant) anyway, I realized why you had the constructor private, but I find I can do all that stuff in the factory.
That way I can use the constructor if I need. – Daniel Oct 29 '09 at 23:43.
You could use a factory method to create the object: private function __construct($input = null) { } public static function create($input = null) { if (empty($input)) { return false; } return new MyObject($input); }.
I beat you by 3 seconds :) – philfreo Oct 29 '09 at 23:06 Great minds... :) – jheddings Oct 29 '09 at 23:08 1 and I believe that your create method should be a static method, like in my solution. – philfreo Oct 29 '09 at 23:08 IIRC, PHP will still recognize the call, but putting static is more explicit and ensures no '$this' pointer. – jheddings Oct 29 '09 at 23:11.
I believe you are correct. A print_r of your new object will show it returns an object. You could make it throw an exception, but that's unlikely to be the style you want.
+1 on factory methods already posted. Those factory methods might also be like: public static function newCreate( $a ) { return (!$a )? False : new foo( $a ); } I like factory methods :-).
But I would like to show the power of parent::__construct for use with PHP's OOP polymorphic behavior (you'll see what this is very quickly). In my example, I have created a fairly robust base class that does everything that all subclasses need to do. Here's the base class def.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.