Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Older PHP version fix

Tags:

php

Trying to dynamically load a class:

    require_once(PATH_MODULES."/{$module}/{$module}_admin.php");        
    $admin_class = $module."Admin";     
    return $admin_class::get_admin($module);

Produces this error on older versions of PHP:

Parse error: syntax error, unexpected
T_PAAMAYIM_NEKUDOTAYIM in 
/home/user/public_html/folder/path/admin/filename.php on line 91 

How can I change this code to work for older versions of PHP?

like image 344
tmartin314 Avatar asked Apr 14 '11 20:04

tmartin314


2 Answers

The problem as you probably expected, is that you cannot use dynamic class-names in PHP < 5.3. That's why the :: is unexpected after the variable.

I don't see any way to go around this. You're not allowed to do this:

 $admin_class::get_admin($module);

If this part is always the same:

$admin_class = $module."Admin";     
return $admin_class::get_admin($module);

You could (and this is a hack!) add these strings to that module with the module name filled in ofcourse. Or make a separate file for that?

So for module "yourModule" you add to the "/yourModule/yourModule_admin.php" file these lines:

$admin_class = "yourModuleAdmin";     
return yourModuleAdmin::get_admin($module);

Or add a separate file that you call yourModule_admin.olderversions.php

Not too pretty, I agree.

like image 116
Nanne Avatar answered Nov 04 '22 04:11

Nanne


In PHP <5.3 you can use call_user_func:

return call_user_func(array($admin_class, 'get_admin'), $module);
like image 20
NikiC Avatar answered Nov 04 '22 04:11

NikiC