Jessica Posted April 27, 2012 Share Posted April 27, 2012 I have this as my autoloader. All the classes are based off of Class O. I wanted to be able to do something like $dog = new Dog(); even if I hadn't yet created Dog.class.php and made it extend O. (I realize there are plenty of reasons this is a bad idea, this is a temporary part of the project, I am just trying to get a few things set up and thought it would work - now I'm probably going to spend more time trying to do this than it would have saved, but I'm curious. ) ( ! ) Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in C:\wamp\www\project\index.php on line 22 (line 22 is Class $class_name extends O{ <?php function autoload($class_name) { if(file_exists($class_name.'.class.php')){ require_once($class_name.'.class.php'); }else{ Class $class_name extends O{ } } } ?> Is it possible to do what I'm trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/ Share on other sites More sharing options...
xyph Posted April 27, 2012 Share Posted April 27, 2012 I don't think this is possible without using eval() You might also want to check if class_exists before creating it. Anonymous classes don't yet exist in PHP. <?php class o { public function foo() { echo 'bar'; } } $name = 'dog'; if( !class_exists($name) ) eval( 'class '.$name.' extends o {}' ); $obj = new $name; $obj->foo(); // outputs 'bar' ?> Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341230 Share on other sites More sharing options...
cpd Posted April 27, 2012 Share Posted April 27, 2012 No. The purpose of the autoload function is to include the class file before instantiating the class. Its not to create classes. You need to think of classes as templates which are pre-mdae. Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341234 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Author Share Posted April 27, 2012 I don't think this is possible without using eval() You might also want to check if class_exists before creating it. Anonymous classes don't yet exist in PHP. Duh, eval()!! Thank you! Not at the desk anymore but I will try that as soon as I get back. Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341235 Share on other sites More sharing options...
xyph Posted April 27, 2012 Share Posted April 27, 2012 No. The purpose of the autoload function is to include the class file before instantiating the class. Its not to create classes. You need to think of classes as templates which are pre-mdae. She realizes this isn't the right thing to do, as stated in the OP. I'm guessing this is a temporary solution to test functionality for parameters that might not have classes to parse them yet. Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341238 Share on other sites More sharing options...
cpd Posted April 27, 2012 Share Posted April 27, 2012 I'm missing your point xyph. I just gave a perfectly valid explanation in answer to the question "Is it possible to do what I'm trying to do?" even if it is technically possible. Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341246 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Author Share Posted April 27, 2012 eval worked perfectly to do it in the autoloader. <?php function autoload($className) { if(file_exists($className.'.class.php')){ require_once($className.'.class.php'); }else{ $dynamicClass = "class $className extends O{}"; eval($dynamicClass); } } ?> And yes, this is a very temporary thing. I'm doing it in the autoloader because otherwise if I'm doing to take the time to define the name of the class before trying to load it, I might as well take the time to make the 1 line file which would then be autoloaded. It is possible, just needed to use eval(); Much thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/261728-generating-a-class-definition-from-a-variable-name/#findComment-1341252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.