Re: Question on proper proceedure - passing objects to a constructor method

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

 



Nevermind.  I got my answer here:

http://fabien.potencier.org/article/11/what-is-dependency-injection

--- In php-objects@xxxxxxxxxxxxxxx, "m_serix_dm" <gentlemike2@...> wrote:
>
> I have a request date object I use to log errors, activity, and produce date information for my webpages.  I need to pass the date object and/or access to methods of the date object to my error logging class.  Due to scoping issues, I can do one of the following within my constructor method:
> 
> 1) make the request date object global (which defeats the versatility of the code).
> 2) pass the request date object as an argument of the constructor for the error logging object, without making it an attribute of the error logging object,
> or...
> 3) pass the request date object to the constructor and then make them attributes of the error logging object, as in:
> 
> class error_logger{
> protected $error_message;
> protected $request_date;
> 
> function __construct($error_message, $request_date) {
> $this->error_message = $error_message;
> $this->request_date = $request_date;
> /* Remainder of constructor function here. */
> }
> }
> 
> Which is the proper way to do this, according to OOP theory?  Would there be a better way of doing this?  It seems that the first two work pretty well, I haven't tried the third method yet.  The problem is that either way the code is dependent on the pre-existance of the request date object, which limits the usage of my error logger object.
> 
> Suggestions?
> 
> Sincerely,
> 
> Gentlemike2
>



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux