Solved

Using a variable for the class name of a Singeton

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now