Re: Constructors with multiple methods
| From: | Jarrel Cobb | Date: | Thu, 12 Apr 2007 04:55:43 +0000 |
| Subject: | Re: Constructors with multiple methods | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252699@lists.php.net to get a copy of this message | ||
"creating from scratch or loading from a database" sounds like you want a
registry or service locator. Otherwise, you should encapsulate object
instantiation in factory methods/classes
On 4/11/07, Damien <damien.b@koalatelecom.com.au> wrote:
Hi all, Say you may want to create an instance in multiple ways, such as creating from scratch or loading from a database. Is something like this normal for creating an instance? class Foo { function Foo($method) {switch ($method) { default: $method = 'create'; case 'create': case 'load': $args = func_get_args(); array_shift($args); if (!call_user_func_array(array(&$this, $method), $args)) { $this = false; } }} function create()... // Returns false on failure; true otherwise.function load($id)... // Returns false on failure; true otherwise.function save()... // Saves to database.} What are other people doing to instantiate their objects? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php