简体   繁体   English

较旧的PHP版本修复

[英]Older PHP version fix

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: 在旧版本的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? 如何更改此代码以适用于旧版本的PHP?

The problem as you probably expected, is that you cannot use dynamic class-names in PHP < 5.3. 您可能期望的问题是,您不能在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: 因此,对于模块"yourModule"您将这些行添加到"/yourModule/yourModule_admin.php"文件中:

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

Or add a separate file that you call yourModule_admin.olderversions.php 或者添加一个名为yourModule_admin.olderversions.php的单独文件

Not too pretty, I agree. 不太好,我同意。

在PHP <5.3中,您可以使用call_user_func

return call_user_func(array($admin_class, 'get_admin'), $module);

Answer was pretty simple: 答案很简单:

    $admin_class = $module."Admin";

    $oAdmin = new $admin_class;

    return $oAdmin->get_admin($module);

I would say, go with a factory pattern similar to what you have done in your own answer. 我会说,使用类似于你在自己的答案中所做的工厂模式。

One way would be 一种方法是

static class ModuleFactory
{
  static public function get($moduleName) {
    if(file_exists($moduleName)) {
      $module= new $moduleName;
      return $module;
    }
    else {
      throw new Exception("Unkown module $moduleName");
    }
  }
}

Then you do 然后你做

$fooBar = ModuleFactory::get("foobar");

I don't like string identifiers and try to avoid them when ever possible. 我不喜欢字符串标识符,并尽可能避免使用它们。 So if you already knew all the modules you could do 所以,如果你已经知道你可以做的所有模块

static class ModuleFactory
{
  const FOO = "foo";
  const BAR = "bar";

  static public function get($moduleName) {
    if(file_exists($moduleName . '.php')) {
      $module= new $moduleName;
      return $module;
    }
    else {
      throw new Exception("Unkown module $moduleName");
    }
  }
}

And you would then be able to do 然后你就可以做到了

$fooBar = ModuleFactory::get(ModuleFactory::FOO);

This would reduce the chance of getting the spelling wrong. 这样可以减少拼写错误的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM