Re: Constructors with multiple methods

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"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@xxxxxxxxxxxxxxxxxxx> 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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux