Singleton pattern vs var access problem
Posted: Wed Jul 21, 2010 7:29 am
Hi
I'm new to PHP, but not to OOP.
Well, I try to create a Singleton pattern Class that will manage my labels for a website. All labels are in an XML file to be easily modify in the future. The Class works, I can get an instance of it, it loads the XML file load properly, etc. My problem is when I try to access the $xmlFile from another method. I can display the value of the XML file directly after the load in the __construct() method, but not in the getLabel() function.
About the LanguagesManager Class, it is a Class that I don't want to be able to extends, so it is a final class, I want it to be a Singleton Pattern Class, so the only way to create a instance is using the getInstance(), and after that, I want to be able to get data from the XML using the getLabel() function.
I tried these test in the getLabel but non of them works (Assume that from a PHP file I create an instance and try to call the function.)
echo $xmlFile;
echo $this->$xmlFile;
echo self::$xmlFile;
Any idea how to do that.
Thanks
Code example
I'm new to PHP, but not to OOP.
Well, I try to create a Singleton pattern Class that will manage my labels for a website. All labels are in an XML file to be easily modify in the future. The Class works, I can get an instance of it, it loads the XML file load properly, etc. My problem is when I try to access the $xmlFile from another method. I can display the value of the XML file directly after the load in the __construct() method, but not in the getLabel() function.
About the LanguagesManager Class, it is a Class that I don't want to be able to extends, so it is a final class, I want it to be a Singleton Pattern Class, so the only way to create a instance is using the getInstance(), and after that, I want to be able to get data from the XML using the getLabel() function.
I tried these test in the getLabel but non of them works (Assume that from a PHP file I create an instance and try to call the function.)
echo $xmlFile;
echo $this->$xmlFile;
echo self::$xmlFile;
Any idea how to do that.
Thanks
Code example
Code: Select all
<?php
final class LanguagesManager
{
protected static $_instance;
private $xmlFile;
protected function __construct()
{
$xmlFile = simplexml_load_file("http://" . $_SERVER["SERVER_NAME"] . "/xml/siteLanguages.xml");
}
protected function __clone() {}
public static function getInstance()
{
if(self::$_instance === NULL) { self::$_instance = new LanguagesManager(); }
// Return the instance reference
return self::$_instance;
}
/***
* Must be completed.....
**/
public function getLabel()
{
echo $xmlFile;
echo $this->$xmlFile;
echo self::$xmlFile;
}
}
?>