Solved

Using a variable for the class name of a Singeton

Posted on 2008-10-19
3
1,552 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

776 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