Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
in all of the methods that need to use it in all of the classes that have such methods. Another - passing the array itself to methods. Both ways look awful to me. Is there a nice and painless way to achieve the functionality I need?
you could grab them from the superglobals, but it's advised you pass the information in some fashion. Alternately, a singleton could work as well.. which is basically the same but the data is stored in an object instead.
class LanguageData
{
function LanguageData()
{
}
function getString($key)
{
include('en.php');
return $lang[$key];
}
}
class UserClass
{
function UserClass()
{
}
function needsLanguageInfo()
{
//code
$string = LanguageData::getString(256);
}
}
Why would I want to use singleton if I can simply access the language string I need via class::method()? It seems to solve my problem, but maybe singleton has advantages I'm not aware of?
define('LANG', 'en.php');
class LanguageData
{
var $lang;
function LanguageData($file)
{
include($file);
}
function &Singleton($file)
{
static $object;
if (!isset($object))
{
$object = new LanguageData($file);
}
return $object;
}
function getString($type, $number)
{
return $this->lang[$type][$number];
}
}
class Test
{
var $lang;
function Test()
{
$this->lang = &LanguageData::Singleton(LANG);
}
function needsSomeString()
{
$string = $this->lang->getString('type', 5);
//do stuff
}
}