Jump to content

Generating a class definition from a variable name


Jessica

Recommended Posts

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?

Link to comment
Share on other sites

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'

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.