View Full Version : Variable variables and creating objects

08-04-2008, 10:12 PM
I have this method for creating classes and using them to echo a DOM element

* This method creates the pane and finds it based on the classpath and the class name.
* CAUTION: Uses an Object Factory!
* @param $div - the id for the pane
public function createPane($div){
$classname = explode("_",$div);
$classes = array();
foreach($classname as $class){
$classes[] = ucfirst($class);
$classname = (implode($classes, "_"));

$object = new $classname();
$tmp = $object->getTitle(); //empty cannot read return values :(
$title = $tmp;
if($object->getTitle() == ""){
$title = $object->getId();
if($tmp == '-1')
$title = "";
$pane = "\n<div class='pane'><dl class='sort'>";
$pane .= $object->getBar();
$pane .= "\t<dd><div class='$div'>";
$pane .= $object->output();
$pane .= "\n\t</div></dd>\n</dl></div>";

return $pane;

But it doesn't seem to work on IIS, it works fine on Apache, but I don't think that's the issue.
It fails silently at $object = new $classname(); Does anyone know why it would do this? Could it be php's configuration?

08-05-2008, 10:20 AM
Is it that its not working on IIS, or that IIS doesn't have the necessary libraries installed? I'm also assuming that you're using this to factory up some built in objects like mssql or mysql or whatever.
Before you're object creation, try a:

if (!class_exists($classname))
throw new Exception(sprintf("Class %s cannot be found!", $classname));

This is especially the case with something like DOMDocument creation. I find it tends to be hit and miss on servers as to whether it has been loaded or not. Granted, it is by default enabled in PHP5+, but that certainly doesn't mean it cannot be disabled.

08-05-2008, 02:31 PM
It doesn't use a special library. It's only creating the objects that I wrote. They extend a class called Pane and the create a DOM object that goes into the webapp.

If, say it can't find the class, it will throw an error that it couldn't find it, which it does when I feed it the wrong name on purpose, however, it doesn't give any errors and the ajax request to create the object never returns. Not exactly a 404 but Firebug highlights it in red and the response is always 'Loading...'

08-05-2008, 02:38 PM
I didn't look too closely at what you were doing, but maybe it has to do with this:

$object = new $classname();

Should it not be:

$object = new classname();


08-05-2008, 02:50 PM
No, because $classname is a variable, and the variable's value is the name of the object to be created. I.E. It's a variable variable, almost.

It's call an object factory, I can pass the factory a name of a class, and it can create the class and return it to me. It's a very interesting idea, and very useful in may case. Here I have a bunch of classes that may need to be created at anytime and they all extend the same class. However I don't know which classes are going to be called, so when they are, they get passed to this method with creates them for me and I use the methods that they all inherit, although the perform differently, to utilize them.

08-05-2008, 05:10 PM
In that case, I think you need to use eval (http://us3.php.net/eval)

class myclass {
public function __construct($in) {
print "The variable supplied was $in\n";
public function __tostring() {
return "This is a call to the tostring() function\n";
$classname = "myclass";
$eval_string = '$object = new '.$classname.'("Hello World");';
print $object;

08-05-2008, 07:01 PM
I'll try that, I won't be able to test it on the IIs (where it isn't working) today because the boss is out and she's the only one with access to it.

08-05-2008, 11:53 PM
Eval is unnecessary, thats not the problem. PHP has no problem with generating an object with a new $varname call so long as the class exists for it. If you really wanted to you can run a call_user_func against it instead to call the __construct method.
Did you try to check the existence of the class before loading it? And for you're dom, are you referring to a custom created dom or a buitin dom like DOMDocument or XML? These can both be disabled in PHP, so its always good to check if they exist first.
I assumed that you're: require_once(INTRA_PANES_AB."/".$div.".inc.php"); call includes all the necessary classes included within it.
I can't remember if I asked you this already, but are you generating any errors when you try to run it? You may need to check you're error logs or enable you're display with:

ini_set('display_errors', 1);

08-11-2008, 10:00 PM
The problem turned out to be Gzipping the page. Supposedly IIS supported it but it failed silently whenever a page was gzipped.