dpl22899
2013-07-02 22:48
浏览 228
已采纳

使用spl_autoload_register在运行时加载类将导致性能不佳?

Well if i do something like:

boot.php:

function boot($c) { require 'mods/'.$c.'.php'; }
spl_autoload_register('boot');

index.php

require 'boot.php';

class Father {
function __construct()
{
/* get all modules in database then loop it like: */
foreach($mods as $v) eval('$cmod = new '.$v.'()');
}
}
new Father();

Example of a class module:

class mod01 extends Father {

function __construct() { //code }

}

I would like to know if use eval is a good or bad point, i'm using it cuz i don't know the name of mods it will come of db.

图片转代码服务由CSDN问答提供 功能建议

好吧,如果我这样做:

boot.php:

  function boot($ c){require'mods /'.$ c。'。php';  } 
spl_autoload_register('boot'); 
   
 
 

index.php

  require'boot.php';  
 
class父{
_function__construct()
 {
 / *获取数据库中的所有模块然后循环它:* / 
foreach($ mods as $ v)eval('$ cmod = new'。$ v  。'()'); 
} 
} 
new父(); 
   
 
 

类模块示例:

  class mod01扩展了父{
 
 
函数__construct(){//代码} 
 
} 
   
 
 

我想知道 如果使用eval是一个好的或坏的点,我正在使用它因为我不知道它将来自db的mod的名称。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxuanchao1425 2013-07-02 22:50
    已采纳

    you don't need eval(). (and if not necessary, simply don't use it)

    foreach($mods as $v)
        $cmod = new $v();
    

    Works too.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhuang1923 2013-07-02 22:52

    You could do it either way and still have access to the newly created classes.

    $classes = array() ;
    foreach($mods as $v){
      $classes[] = new $v(); //Whats the point of rewriting $cmod?
    }
    

    You just rewrite reference to objects in each iteration, so store your references in an array.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题