...

View Full Version : Variable variables and creating objects



malfist
08-04-2008, 09: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){
require_once(INTRA_PANES_AB."/".$div.".inc.php");
$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?

Fou-Lu
08-05-2008, 09: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.

malfist
08-05-2008, 01: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...'

derzok
08-05-2008, 01: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();


?

malfist
08-05-2008, 01: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.

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



<?php
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");';
eval($eval_string);
print $object;
?>

malfist
08-05-2008, 06: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.

Fou-Lu
08-05-2008, 10: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:


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum