Page 1 of 1

using php classes - scope question & passing variables

Posted: Thu Jan 01, 2009 5:44 pm
by itp
I inherited some php code. The program uses global variables to pass around arrays of information. It work, but it seems to not be in the spirit of OO programming. Here is example that illustrates my point. Can someone propose a better way to access variables in class functions, outside the scope of the function?

Code: Select all

<?php
class clRegistration 
{
    var $gblFormvars ;          
 
    function fnShowForm()
    {   
        global $gblFormvars;
        $q = "'";   
        $dq = "\"";   
        $comma = ", "; 
    
        echo "<form method='get' action=" .$q . $_SERVER['PHP_SELF'] . $q . ">";
        echo "Name ";
        echo " <input type='text' name='registrantName' value=" . $dq . $gblFormvars['registrantName'] . $dq . "\" />";
        echo "<br><input type='submit' name='action' value='submit' />";        
    }   
 
    function fnCleanFormData(&$formvars) 
    {       
        global $gblFormvars;        
        $formvars['registrantName'] = empty($formvars['registrantName'])    ?  ""  :  $formvars['registrantName'];
        $gblFormvars = $formvars;       
    }
}
    
    $RegistrationObj = new clRegistration;  
    $RegistrationObj->fnCleanFormData($_REQUEST);
    $RegistrationObj->fnShowForm();
 
?>

Re: using php classes - scope question & passing variables

Posted: Thu Jan 01, 2009 6:23 pm
by kaszu
Use $this->gblFormvars
Check PHP manual

Re: using php classes - scope question & passing variables

Posted: Thu Jan 01, 2009 10:35 pm
by itp
OK. Got it.
It looks cleaner, but $gblFormvars is still a global. Can I do more?

Code: Select all

 
<?php
class clRegistration 
{
    var $gblFormvars ;          
 
    function fnShowForm()
    {   
        $q = "'";   
        $dq = "\"";   
        $comma = ", "; 
    
        echo "<form method='get' action=" .$q . $_SERVER['PHP_SELF'] . $q . ">";
        echo "Name ";
        echo " <input type='text' name='registrantName' value=" . $dq . $this->gblFormvars['registrantName'] . $dq . "\" />";
        echo "<br><input type='submit' name='action' value='submit' />";        
    }   
 
    function fnCleanFormData(&$formvars) 
    {   
        $this->gblFormvars['registrantName'] = empty($formvars['registrantName'])   ?  ""  :  $formvars['registrantName'];
    }
}
    $RegistrationObj = new clRegistration;  
    echo $gblFormvars['registrantName'] ;
    $RegistrationObj->fnCleanFormData($_REQUEST);
    $RegistrationObj->fnShowForm();
 
?>
 
 

Re: using php classes - scope question & passing variables

Posted: Fri Jan 02, 2009 5:44 am
by pkbruker
Generally, the whole idea of global variables is bad. For instance, it's hard to keep track of where in the code global variables are modified since the entire application has access to them.

The best thing would be to redesign the classes so that they do not use global variables. Pass the required variables to the function either as a parameter to the function itself or as a parameter in the constructor.

However, if this redesign takes a lot of time, you might want to consider keeping the global variables in order to save time. A possible middle way could be to put all the global variables in a class, in order to have a common "gateway" through which all global variable access is controlled. Make this class itself global, and required modification in existing code should be some rather simple replacing.

Good luck!