Question

I am using a ge_called_class hack for allowing late static binding in php version 5.2 (found here).

I have the following in my code:

# db_record.php
$ac = "ForumThread";
$objects = $ac::find("all");

This will not work in php 5.2 for some reason, so I have done this:

# db_record.php
$ac = "ForumThread";
eval("\$objects = {$ac}::find('all');");

This on the other hand will not work with the get_called_class function. I get an error that the file function can't read the evaled section of code.

Was it helpful?

Solution

If you're using eval, your solution is wrong.

Why won't your non-eval version work? What is going wrong? What is the full and complete error message?

The user-suppled version of get_called_class performs a backtrace and tries to open the caller's file to determine the class name. The reason the eval fails is because the eval backtrace doesn't supply a filename.

(Edit: Also, that get_called_class hack is very much a hack. Is there a reason you can't use 5.3?)

Have you tried using call_user_func? call_user_func(array($ac, 'find'), 'all') should call the static method find for the class name contained in $ac with the paramater 'all'. See also the callback pseudo-type, and the "Type 2" example in specific

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top