using php classes - scope question & passing variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

using php classes - scope question & passing variables

Post 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();
 
?>
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: using php classes - scope question & passing variables

Post by kaszu »

Use $this->gblFormvars
Check PHP manual
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

Re: using php classes - scope question & passing variables

Post 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();
 
?>
 
 
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: using php classes - scope question & passing variables

Post 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!
Post Reply