views:

74

answers:

1

Hi there.

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.

So how do I solve this problem?

Best regards.

A: 

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

Charles
I totally agree that it shouldn't be necessary to use `eval` but it so happens it works on my own box with php 5.3, but not on the server with 5.2. Running it will trigger this error:Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/virtual/atanea.dk/vendor/framework/lib/db_record.php on line 89Fatal error: Call to a member function assign() on a non-object in /home/virtual/atanea.dk/vendor/framework/lib/action_view.php on line 87`
Ekampp
And as to why I'm not using 5.3 is because my hosting company doesn't support it yet with the argument that it's still unstable. So I'm forced to do all the hacks.
Ekampp
Still unstable? It's been out for a year this month. Your host is full of it. As for the error, 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'`. (This has been integrated into my answer.)
Charles
No, I havn't tried that. I will try and see if that fixed the problem. I even knew that existed, but didn't think to use it. Sometimes it simply helps to bounce of ideas on others :) Thanks!
Ekampp
I'm assuming from the accept that it works. That's good to know! :)
Charles
Yep. It worked nicely. Although, I still can't figure out why the error occurs in the first place.
Ekampp
I'm not sure either. This works in PHP 5.3: `class What { static public function the() { echo "censored!"; } } $lol = 'What'; $lol::the();`
Charles