Constructors with multiple methods

From: Date: Thu, 12 Apr 2007 02:49:13 +0000
Subject: Constructors with multiple methods
Groups: php.general 
Request: Send a blank email to php-general+get-252698@lists.php.net to get a copy of this message
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?

« previous php.general (#252698) next »