Dynamic class name

Hi, how do execute dynamic class in PHP? For example with these values coming from the database:

include( ROOT . $value['folder'] . '/' . $value['class'] . '.class.php' );
$Plugin = new $value['class'];
$Plugin->$value['method'];

Both $value[‘class’] and $value[‘method’] are string stored in the database, I got the error Fatal error: Class ‘PersonalClass’ not found.

Any help is greatly appreciated.

And what if you remove the include stuff and just stub a class at the top of the file temporarily?


<?php

class PersonalClass {}



Hi Cups, yes it works fine if i hardcode it. :confused:

Well it would help if you didnt have a syntax error :wink:

Hint: new classnameCOLOR=“#FF0000”[/COLOR]

Hi SL, its not a syntax error, you can do without the parenthesis.

ROOT . $value['folder'] . '/' . $value['class'] . '.class.php'

The class path is likely not resolving and correctly declaring the class???

Regards,
Alex

Hi Alex, the file is being included right because like Cups mentioned, using hardcoding works.

What version of PHP are you using? I cannot currently find it but previous versions of PHP had trouble with dynamic initializations of objects. If all else fails there is reflection. http://us.php.net/manual/en/book.reflection.php

So what if you temp hardcode the actual path to the class as well:


// include( ROOT . $value['folder'] . '/' . $value['class'] . '.class.php' );
include( '/path/to/PersonalClass.class.php' );

5.2 is the version that began allowing dynamic class/method referencing, IIRC… It’s certainly been in since then.

Edit: I was thinking of 5.3, and that was for Static methods. Not sure when variable class instantiation was added. Still, if you’re not running 5.3+, might be wise to upgrade.

Actually I think cups asked you to hardcode the class name precisely because he suspected the file was not being included. So I would check the path to the class file.

@abdussamad - yeahm thanks, I wish I had put it that clearly. :wink:

Firstly as others are suggesting, only troubleshooting for you is; either just try once including the class file temporarily hardcoded or to echo/print the values of each element of the array $value[‘folder’], $value[‘class’], $value[‘method’] including ROOT:


echo ROOT . $value['folder'] . '/' . $value['class'] . '.class.php';

Does this really shows the correct value? If yes there should (must) not be the problem if you are in the PHP version of having Dynamic use of Class Names. Otherwise there is nothing to explore in your code and don’t have to pull out your hair. As per my knowledge, you are on the right track. Better you let us know the PHP version that you are in and at least try to test what the guys have suggested here.

Furthermore, I would recommend you to use require() instead of include() since require() produce FATAL error and halts the script while include will continue with an E_WARNING and maybe you have set error_reporting something to hide warnings. So try once this as well:


error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
require ROOT . $value['folder'] . '/' . $value['class'] . '.class.php';

Secondly I would recommend you to use Autoloading Classes in PHP which you can find here http://php.net/manual/en/language.oop5.autoload.php with some examples. If you go for it, you don’t have to worry about if it is included or not it will automatically include when you create an instance of a class:


$Plugin = new $value['class'];