Re: Re: Scope woe
| From: | Eddie Drapkin | Date: | Tue, 30 Jun 2009 15:17:17 +0000 |
| Subject: | Re: Re: Scope woe | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294748@lists.php.net to get a copy of this message | ||
On Tue, Jun 30, 2009 at 11:12 AM, Peter Ford<pete@justcroft.com> wrote:
> Luke wrote:
>>>
>> Thanks for the replies :)
>>
>> Surely if I pass it as a parameter to the __construct then I would have to
>> make an instance of the otherObject, notice that messwithotherthings is
>> static?
>>
>> Also, if I'm not using OOP properly, Eddie, how would I use it properly to
>> prevent this situation?
>>
>> Thanks,
>>
>
> Hmmm, I didn't notice the method was static - that means my idea really won't
> work...
>
> I did have a bad feeling about this, and Eddie confirmed my unease with the
> point about OOP.
>
> Really, you should need your $firstobject to be a singleton:
> eg.
>
> class FirstObject
> {
> private static $theInstance = NULL;
>
> public $propertyContainingObject;
>
> protected function __construct()
> {
> // whatever
> }
>
> public static function getInstance()
> {
> if (!self::$theInstance)
> {
> self::$theInstance = new FirstObject();
> }
> return self::$theInstance;
> }
>
> // ... other methods ...
> }
>
>
> So then:
>
> class OtherObject
> {
> static function messWithOtherThings()
> {
> $firstObject = FirstObject::getInstance();
> $firstObject->propertyContainingObject->methodInsideThatObject();
> }
> }
>
>
>
> I think that works, and is reasonable to the OOP purists...
>
>
> --
> Peter Ford phone: 01580 893333
> Developer fax: 01580 893399
> Justcroft International Ltd., Staplehurst, Kent
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I wouldn't really recommend going with a singleton in this situation,
as there exists a different solution (my other post :P) and there are
very few actual use-cases where the pattern makes sense, and deploying
it unnecessarily can cause a whole lot of headache D: