Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts

    Variable variables and creating objects

    I have this method for creating classes and using them to echo a DOM element
    PHP Code:
    /**
         *    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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...'

  • #4
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    I didn't look too closely at what you were doing, but maybe it has to do with this:
    PHP Code:
            $object = new $classname(); 
    Should it not be:
    PHP Code:
            $object = new classname(); 
    ?
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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.
    Last edited by malfist; 08-05-2008 at 02:52 PM.

  • #6
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    In that case, I think you need to use eval

    PHP Code:
    <?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;
    ?>
    Last edited by derzok; 08-05-2008 at 05:13 PM.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL);
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts
    The problem turned out to be Gzipping the page. Supposedly IIS supported it but it failed silently whenever a page was gzipped.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •