Singleton pattern vs var access problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JFFortier
Forum Newbie
Posts: 1
Joined: Wed Jul 21, 2010 7:09 am

Singleton pattern vs var access problem

Post by JFFortier »

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

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;
	}
}
?>
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Singleton pattern vs var access problem

Post by PHPHorizons »

Hello JFFortier,

You needed to use $this->xmlFile in the constructor:

Code: Select all

        protected function __construct()
        {      
                $this->xmlFile = simplexml_load_file("http://" . $_SERVER["SERVER_NAME"] . "/xml/siteLanguages.xml");
        }
Now the getLabel method can go like this:

Code: Select all

        public function getLabel()
        {
                echo $this->xmlFile;
        }
Cheers

Edit: Fixed the error Weirdan noticed.
Last edited by PHPHorizons on Thu Jul 22, 2010 7:47 am, edited 2 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Singleton pattern vs var access problem

Post by Weirdan »

Now the getLabel method can go like this:
Should be

Code: Select all

$this->xmlFile
, not

Code: Select all

$this->$xmlFile
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Singleton pattern vs var access problem

Post by PHPHorizons »

Thanks Weirdan ;)
Post Reply