| 
 Posted by Jochem Maas on 04/21/05 10:37 
Jason Barnett wrote: 
> Adam wrote: 
>  
>>Hallo again, 
>>thank You for Your response. 
>> 
>> 
>> 
>>>>// singleton for request 
>>>>class Request { 
>>>>   function __destructor() { 
>>>>       $_SESSION["variable"] = "hallo"; 
>>> 
>>>The __destructor() method is supposed to be about killing the class 
>>>(Request).  It's probably bad practice to be changing $_SESSION vars 
>>>inside of there even if that $_SESSION var affects your class.  Here, 
>>>read this page and maybe it will make more sense to you: 
>>>http://php.net/manual/en/language.oop5.decon.php 
>> 
>> 
>>i agree that in *most* cases destructor should only clean up after object. 
>>unfortunatly for "request" class there are few requirements: 
>>1) must allow immidiate use w/o any explicit initialization or finalizaion. 
>>e.g. at any file You can use only Request::getInstance()->isRefresh() /** 
>>test if this request was caused by browser refresh */. this should be one 
>>and only line (except for inclusion of request.php oc) referencing request 
>>class -- no explicit call to Request::saveToSession() or anything ugly and 
>>potentialy disastrous like that. 
>  
>  
> Well ok, but why can't you just have saveToSession() in the conditional 
> block? 
>  
> <?php 
>  
> $_SESSION['Request'] = Request::getInstance(); 
>  
 
look $_SESSION['Request'] is now a reference to the Request getInstance(); 
when this session is closed the object will automatically be serialized - no need 
to saveToSession or whatever!.... just make sure you have always declared the Request class 
before you start the session. 
 
so have the Request::getInstance() deal with checking/setting $_SESSION['Request'] internally. 
 
> if ($_SESSION['Request']->isRefresh() ) { 
>   $_SESSION['Request']->saveToSession(); 
>   /* etc. */ 
>   exit(); 
> } 
>  
> /* Rest of script follows here */ 
>  
> ?> 
>  
> IMHO the point of OOP is to keep each function split into the minimal 
> functionality that you need.  This allows you to reuse code easier 
> because a function does only the *minimum* amount of work necessary 
> instead of extra (unintended) functionality. 
>
 
[Back to original message] 
 |