NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing Properties in PHP OOP

Scott Mattocks scott at crisscott.com
Thu Aug 26 10:26:33 EDT 2004


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



More information about the talk mailing list