user3065191
user3065191

Reputation: 145

PHP call function in same class (of another file)

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:')

Upvotes: 1

Views: 2668

Answers (1)

Matt Indeedhat Holmes
Matt Indeedhat Holmes

Reputation: 725

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']; 

Upvotes: 1

Related Questions