1

I have two files: index.php and load.php (which is a class).

I'm currently managing the languages. In the index.php I call the language this way:

include_once('load.php'); $load = new load(); include_once($load->language()); 

Then I can just simply use:

echo $lang['name']; echo $lang['address']; //etc 

My question is..how do I use this inside the class load.php? I've tried so far:

include_once($this->language()); $lang['name']; retrieves: Notice: Undefined variable: lang $yo = include_once($this->language()); // retrieves $yo = bool(true) -> but I can't get info from the variable $yo 

The function language() only retrieves the correct file according to the Cookie.

$content = 'languages/en.php'; // or $content = 'languages/fr.php'; return $content; 

EDIT: to be more clear:

languages/en.php

$lang = array(); $lang['name'] = 'Your name:'; $lang['address'] = 'Your address:'; 

index.php

include_once('load.php'); $load = new load(); include_once($load->language()); // $load->language() == 'languages/en.php'; echo $lang['name']; // works echo $lang['address']; // works 

load.php

public function display(){ include_once($this->language()); var_dump($this->lang) // returns NULL var_dump($this->lang['name']) // returns NULL // I call this function in index.php } 

I've tried to use:

return $lang 

without success.

Edit 2: Looking at the answer of @Saurabh Sinha

the thing you are trying is not right. you cannot use the variable declared outside the function unless it is declared in the class. and in your case too $lang variable is outside the class and the function. you are able to access the same from index file just because when the language function is called it gives $lang variable is index file which is not a class

Which is very clear, I've join the chat with him and we conclude that the best alternative would be the define function.

define('name', 'Your name:') 
10
  • try using $this->lang['name']; instead of $lang['name']; in load.php file Commented Jan 21, 2014 at 10:58
  • within the same class you need to use $this to access the properties.. Commented Jan 21, 2014 at 10:59
  • where is the $lang variable initialized ? Commented Jan 21, 2014 at 11:00
  • 1
    @user3065191: the thing you are trying is not right. you cannot use the variable declared outside the function unless it is declared in the class. and in your case too $lang variable is outside the class and the function. you are able to access the same from index file just because when the language function is called it gives $lang variable is index file which is not a class Commented Jan 21, 2014 at 11:30
  • 1
    let us continue this discussion in chat Commented Jan 21, 2014 at 11:30

1 Answer 1

1

If i understand the problem correctly this is how i would go about it.

If in your language pack you return the array of data you can assign it to a class variable.

lang file

return array( 'name' => 'name', 'address' => 'address', ); 

load class

if (file_exists($path)) { $this->lang = (include $path); } 

language pack can then be accessed by $load->lang['name']; etc you should of course initialise the class variable for lang before you use it.

EDIT

if you want to be able to access the langpack from $lang[] you could always pass in that variable by reference to load method

public function loadLangPack(&$langRef) //& before variable means pass a reference { include_once($this->language()); $langRef = $lang; } 

that would allow you to keep using $lang as you have been

index.php

$lang = array(); $load = new Load(); $load->loadLangPack($lang); echo $lang['name']; echo $lang['address']; 
Sign up to request clarification or add additional context in comments.

2 Comments

Can't you help in my way? Or it's impossible?
i think i know what you are doing wrong but its hard to be sure with the limited code you have posted, could you include your full index.php and load.php files or at lest just the load.php file please

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.