View Single Post
Old 10-27-2007, 09:01 PM   #4 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

A member variable is a variable which belongs to a class. In this case it belongs to the class with the getLanguages/getLanguage methods (function). You declare it at the top of the class (see code example below) and then can assign values to it using the [inline]$this->variable_name[/i] syntax. We'll declare the variable as public because we need access to it from outside the class.

PHP Code:
class Lang {
    public 
$phrases = array();
    
// I'm just giving you the general idea, don't just copy/paste!
    
public getLanguage(...) {
        include(...);
        
$this->phrases $lang;
    }

You can then access that variable from the instance of the class.

PHP Code:
$pLanguage = new Lang();
$pLanguage->getLanguage('french');
echo 
$pLanguage->phrases['first_page'];

// Alternatively, if you want to keep the $lang['...'] style
$pLanguage = new Lang();
$pLanguage->getLanguage('french');
$lang = &$pLanguage->phrases;
echo 
$lang['first_page']; 
Finally, with reference to my 'hackish' comment. I really, really don't like assigning values to the $GLOBALS array and as a result assigning global variables ($GLOBALS['myvar'] is the same as [inline]$myvar[/i] in the global scope). It's like using global $myvar in PHP4 functions, urgh.
Salathe is offline  
Reply With Quote