TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Need help with array() (http://www.talkphp.com/general/5670-need-help-array.html)

Kingdom 01-02-2011 08:15 PM

Need help with array()
 
OK, so i have this class:
PHP Code:

<?php
class Lang extends Db {
    var 
$lang = array();
    var 
$chosen_lang;
    var 
$lang_dir;
    var 
$img_dir;
    
    function 
setLang ($chosen_lang) {
        
$this->lang_dir $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/';
        
$this->img_dir $_SERVER['DOCUMENT_ROOT'].'/gl_imgs/'.$this->chosen_lang.'/';
        
$this->chosen_lang $chosen_lang;
        
        unset(
$chosen_lang);
    }
    
    public function 
loadLang ($file){
        if( 
file_exists$_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' ) ) {
            include( 
$_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' );
            
$this->lang[$file] = array();
                
            foreach (
$lang as $k => $v) {
                
$this->lang[$file][$k] = $v;
            }
        }
        
        unset(
$lang$file$k$v);
    }
}
?>

And on the index page of the website i have this:
PHP Code:

<?php
include($_SERVER['DOCUMENT_ROOT'].'classes/class_Db.php');
include(
$_SERVER['DOCUMENT_ROOT'].'classes/class_Lang.php');

$lang = new Lang;
$lang->setLang('en-us');
$lang->loadLang('website_account');
$lang->loadLang('website_main-menu');
?>

My question is: why the script won't work if i add the command:
PHP Code:

$lang->loadLang('website_main-menu'); 

If i only leave the following, the script works as intended:
PHP Code:

$lang = new Lang
$lang->setLang('en-us'); 
$lang->loadLang('website_account'); 

After some debugging, i found out that the script executes the first loadLang command on the 'website_account', but when it executes the second loadLang command ('website_main-menu'), it freezes at this line on the class:
PHP Code:

$this->lang[$file] = array(); 

Thanks in advance!

tony 01-03-2011 12:53 AM

I am guessing is a mixture of document_root and unsetting the $chosen_lang. There is ambiguity in which one you are unsetting since the class's property is named the same and can be access the same. document_root sometimes doesn't work right, that has been my experience, instead I use dirname(__FILE__) for that.
And using var to set class properties is deprecated, you can use public, private, or protected. you can leave it just like that e.g. $lang=array(); and it will default to public.

I am guessing the problem is that it can't include the right file or maybe that file has something that is causing the freezing in this line include( $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' );

Kingdom 01-03-2011 02:00 AM

Oh, yes yes... i found the bug. Thanks a lot, :P.

The problem was that the included file was like this:
PHP Code:

<?php
$lang 
= array(
    
'home' => 'Home'
    'contact' 
=> 'Contact'
    'aboutus' 
=> 'About Us'
    'games' 
=> 'Games'
    'movies' 
=> 'Movies'
    'tv' 
=> 'TV'
    'music' 
=> 'Music'
);
?>

The problem was that i forgot to separate the indexes with the comma, that was why one file was working (had commas) and the other wasn't (no commas). It should look like this:
PHP Code:

<?php
$lang 
= array(
    
'home' => 'Home',
    
'contact' => 'Contact',
    
'aboutus' => 'About Us',
    
'games' => 'Games',
    
'movies' => 'Movies',
    
'tv' => 'TV',
    
'music' => 'Music'
);
?>

Thanks, :D!

BTW, i'll take a look at those problems you mentioned... ^^

tony 01-03-2011 03:04 AM

Great that you find the problem, good thing it wasn't the constructing the lang path. I didn't mean to look like big problems, it is just a have a pet peeve with ambiguities in codes


All times are GMT. The time now is 01:02 PM.

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