Solved

Using a variable for the class name of a Singeton

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

747 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

13 Experts available now in Live!

Get 1:1 Help Now