TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-27-2007, 06:01 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default 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?
Tanax is offline  
Reply With Quote
Old 10-27-2007, 06:36 PM   #2 (permalink)
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

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.
Salathe is offline  
Reply With Quote
Old 10-27-2007, 06:55 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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!
Tanax is offline  
Reply With Quote
Old 10-27-2007, 09:01 PM   #4 (permalink)
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
Old 10-27-2007, 09:49 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 10-27-2007, 09:58 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design