TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Error in my class (http://www.talkphp.com/advanced-php-programming/1347-error-my-class.html)

Tanax 10-27-2007 06:01 PM

Error in my class
 
I don't know what's wrong. I had this script before, but without classes, and just regular good old PHP style. And it worked fine with my language system.

Basicly, it's just including a language file depending on the value in the cookie set...

The language file itself includes a basic array:

english.php
PHP Code:

$lang[error1] = 'Something in english'

swedish.php
PHP Code:

$lang[error1] = 'Something in swedish'

You get the idea. But after I changed the script to OOP, the language system isn't working anymore.

PHP Code:

        // Checks the path for available languages and puts them in an array.
        
public function getLanguages($path) {
            
            if (
$handle opendir($path)) {
        
                while (
false !== ($file readdir($handle))) {
                    
                    if(
$file != "." && $file != "..") {
                        
                        
$file substr($file0, -4);
                        
$this->languages[] = $file;
                        
                    }
                    
                }
                
                
closedir($handle);
                
            }
            
        }

        
// Fetches the current value of $_GET['setlang'] if it's set, otherwise checks cookie if it's set and sets the current language.
        // If not, the standard language is set as the current language.
        
public function getLanguage($path) {
            
            if(
$_GET['setlang']) {
                
                
$language $_GET['setlang'];
                
$check array_search($language$this->languages);
            
                if(
$check !== false) {
                
                    if(
$language == $this->standardLang) {
                    
                        
setcookie('lang'''time()-3600);
                    
                    }
                
                    else {
                    
                        
setcookie('lang'$languagetime()+3600);
                    
                    }
                    
                    
$this->currentLang $language;    
                    include(
$path .$this->currentLang'.php');
                    return 
true;
    
                
                }
            
                else {
                
                    
$this->currentLang 'english';
                    include(
$path .$this->currentLang.'.php');
                    
                    return 
false;
                
                }
                
            }
            
            else {
        
                if(isset(
$_COOKIE['lang'])) {
                    
                    
$this->currentLang $_COOKIE['lang'];
                    
                }
                
                else {
                    
                    
$this->currentLang $this->standardLang;
                    
                }
                
                include(
$path .$this->currentLang'.php');
                return 
true;
                
            }    
            
        } 

And when I use the language, I just use the variable name of the array inside the language files.

For example:

PHP Code:

echo '<a href="'.$_SERVER['PHPSELF'].'?folder='.$this->currentFolder.'&page='.$this->firstPage.'">
                    ' 
.$lang[first_page]. '</a> .. '

As you see:
PHP Code:

$lang[first_page

That would be the value of that key in the array, in the current language file included.
But it's not working, does anyone have any idea how to make a better system perhaps, or how to fix the problem?

Salathe 10-27-2007 06:36 PM

Firstly, please always put quotes around your array keys ($lang['first_page']). Secondly, when you include the language file the variable is only set within the scope of the getLanguage function and is therefore not available globally. There are various ways to solve your problem depending on precisely how you want to handle things.

One solution would be to assign a member variable to the class and then you'd have to access them rather than using a global variable. Alternatively you can just add the line $GLOBALS['lang'] = $lang directly below each of the three include lines. The latter being a hackish and very much frowned upon way of handling things but it would work for you.

Tanax 10-27-2007 06:55 PM

I generally do tend to put quotes around my array keys. But this time I didn't, don't know why.

Anyhow, what do you mean with assigning a member variable?
And what do you mean with "hackish" ? :p


Thanks for your fast reply!

Salathe 10-27-2007 09:01 PM

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.

Tanax 10-27-2007 09:49 PM

I get the idea!
But I can't get it to work within my gallery class for some reason..

I do like you said:
PHP Code:

$lang = new language($language'languages/');
$lang->getLanguages();
$lang->getLanguage();
$language $lang->phrases;
$gallery->setLang($language); 

PHP Code:

public function setLang($lang) {
$this->currentLang $lang;
}

...

echo 
$this->currentLang['next_pic']; 

But it's not working..

PHP Code:

    class language {
        
        public 
$phrases = array();
        private 
$languages = array();
        private 
$path;
        private 
$standardLanguage;
        
        public function 
__construct($standard$path) {
            
            
$this->standardLanguage $standard;
            
$this->path $path;
            
        }
        
        
        
// Checks the path for available languages and puts them in an array.
        
public function getLanguages() {
            
            if (
$handle opendir($this->path)) {
        
                while (
false !== ($file readdir($handle))) {
                    
                    if(
$file != "." && $file != "..") {
                        
                        
$file substr($file0, -4);
                        
$this->languages[] = $file;
                        
                    }
                    
                }
                
                
closedir($handle);
                
            }
            
        }
        
        public function 
getLanguage() {
            
            if(
$_GET['setlang']) {
                
                
$language $_GET['setlang'];
                
$check array_search($language$this->languages);
            
                if(
$check !== false) {
                
                    if(
$language == $this->standardLanguage) {
                    
                        
setcookie('lang'''time()-3600);
                    
                    }
                
                    else {
                    
                        
setcookie('lang'$languagetime()+3600);
                    
                    }
                    
                    include(
$this->path .$language'.php');
                    return 
true;
    
                
                }
            
                else {
                
                    include(
$this->path .$this->standardLanguage'.php');
                    return 
false;
                
                }
            
            }
            
            else {
        
                if(isset(
$_COOKIE['lang'])) {
                    
                    include(
$this->path .$_COOKIE['lang']. '.php');
                    
                }
                
                else {
                    
                    include(
$this->path .$this->standardLanguage'.php');
                    
                }
                
                
                return 
true;
                
            }
            
            
$this->phrases $lang;    
            
        }
            
    } 


=///

Tanax 10-27-2007 09:58 PM

Hm, I noticed that I didn't actually assign $phrases any value.
But I changed it, and it still doesn't work =/


All times are GMT. The time now is 04:23 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0