NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing Properties in PHP OOP

Joseph Crawford Jr. jcrawford at codebowl.com
Thu Aug 26 10:43:52 EDT 2004


better yet, if you want to make things work with php5 as well, do this

class client {
  function client() {
    $this->__construct();
  }

  function __construct() {
    //constructor here
  }

}

Joe Crawford Jr.

----- Original Message ----- 
From: "Scott Mattocks" <scott at crisscott.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
Sent: Thursday, August 26, 2004 10:26 AM
Subject: Re: [nycphp-talk] Accessing Properties in PHP OOP


> randal rust wrote:
> 
> > OK, so I added the client() function, which acts as my constructor. 
> > Then, in my PHP page, I added...
> > 
> > $client->client();
> > 
> > ... which sets everything up so that it can be used by the instance of 
> > the object, right?
> > 
> > I'm guessing so, since it's working.
> > 
> You don't need to call the constructor explicitly. The constructor gets 
> called automatically when you create the instance of the class.  All you 
> need to do is use the new operator and it calls the constructor for you.
> 
> $client =& new Client;
> 
> What you did calls the constructor explicitly after the class is 
> instantiated. If you did something to the class, that modified any of 
> it's properties, between the time you instantiated it and the time you 
> called the constructor explicitly you would loose any of those changes. 
> For instance:
> 
> $client =& new Client;
> $client->display->errors = true;
> print_r($client);
> $client->client();
> print_r($client);
> 
> The output should show that even though you set errors to true it was 
> reset to false when you called the constructor.  That is because it 
> reassigned the display property with a new display object that had the 
> defualt settings.
> 
> Scott Mattocks
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
> 
> 



More information about the talk mailing list