NYCPHP Meetup

NYPHP.org

[nycphp-talk] Constructors and

Matthew Terenzio matt at jobsforge.com
Wed Oct 18 19:34:32 EDT 2006


Just want to bounce this off the OO gurus:

Say you have a User object and sometimes you want to create existing 
users and other times new users.

So you instantiate the object:

$user = new User($username)

and the object recognizes the user and grabs all the user data from the 
DB. . .or

$user = new $User()

and the object creates a new user in the db

but constructors don't return values so there is no way to test that 
the second case in fact succeeded.

So I'm thinking I must create a method like:

public function init() {
      if ( $this->username == NULL) {
          createUser()
     } else {
         //get user data from db with $username
     }
}

so I can test whether my initilaization is successful.


That's fine. Just seems cumbersome to create a new object and ALWAYS 
follow with a given method.
I thought that's what constructors were for.

And I know they are. What's the preferred methodology here?

Thanks




More information about the talk mailing list