Solved

Using a variable for the class name of a Singeton

Posted on 2008-10-19
3
1,556 Views
Last Modified: 2008-10-19
I can use a variable as the class name to create a new instance of the class.  But I can't figure out how to do it to call a function within the class as needed for a singleton.

This works:

$classname='Test';
$classvar=new $classname;
$classvar->setVar("Hello World");
$classvar->echoVar();

This does not:

$classname='Test';
$classvar=$classname::getInstance();
$classvar->setVar("Hello World");
$classvar->echoVar();


How can I use a variable as the name of a class to call a class's function?
Thanks.
<?php
class Test {
	private static $instance;
	private $variable;
	
	public function Test() {
	}
 
	public function getInstance () {
		if(empty(self::$instance)) {
			self::$instance = new Test();
		}
		return self::$instance;
	}
	
	public function setVar($v){
		$this->variable=$v;
	}
 
	public function echoVar(){
		echo $this->variable;
	}
}
?>

Open in new window

0
Comment
Question by:dban00b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Phatzer earned 500 total points
ID: 22753503
Try this (I came across an example on a site called stackoverflow and adjusted it for you):

$classname='Test';
eval("$classvar = " . $classname . "::getInstance()");
var_dump($classvar);
$classvar->setVar("Hello World");
$classvar->echoVar();

You may be able to remove the eval wrapping the script, but I hope this helps either way.
0
 
LVL 1

Author Comment

by:dban00b
ID: 22753550
It needed a little adjustment, but it did the trick.  Can't believe I've never heard of eval() before!  That function will solve so many of my other gotcha's I run into! Thanks alot!

FYI: The "$" has to be escaped, and the eval'ed statement needs a semicolon:

$classname='Test';
eval("\$classvar = " . $classname . "::getInstance();");
//var_dump($classvar);
$classvar->setVar("Hello World");
$classvar->echoVar();
0
 
LVL 10

Expert Comment

by:Phatzer
ID: 22753741
No problem. Will be quite handy for me too :)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question