Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using a variable for the class name of a Singeton

Posted on 2008-10-19
3
Medium Priority
?
1,560 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

926 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