代码之家  ›  专栏  ›  技术社区  ›  Veynom

如何创建一个快速的PHP库?

  •  1
  • Veynom  · 技术社区  · 16 年前

    对于我们的在线游戏,我们在文件和文件夹中编写了大量按主题分组的PHP类和函数。最后,我们将所有后端代码(逻辑和数据库访问层)都保存在一组我们调用的文件中。 伦敦议会大厦 我们在GUI(网页、表示层)中使用 include_once('pathtolib/file.inc') .

    问题是,我们一直懒于包含内容,而大多数include语句都是在libs文件中生成的,这是由于每个网页的结果,每次我们包含任何libs文件时,我们实际上都会逐个文件加载整个libs。

    这对性能有重大影响。因此,最好的解决方案是什么?

    • 从libs文件中删除所有include语句,并且只从网页中调用必需的语句?
    • 做点别的吗?

    服务器使用经典的灯栈(php5)。

    编辑:我们混合了简单的函数(遗留原因和大多数代码)和类。所以自动加载是不够的。

    8 回复  |  直到 16 年前
        1
  •  3
  •   thr    16 年前
        2
  •  3
  •   Max Cantor    16 年前
        3
  •  3
  •   Niranjan N Raju    8 年前

    include include_once

        4
  •  2
  •   Marc Gear    16 年前

    public static function loadClass($class)
    {
        if (class_exists($class, false) ||
            interface_exists($class, false))
        {
            return;
        }
    
        $file = YOUR_LIB_ROOT.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
    
        if (file_exists($file))
        {
            include_once $file;
            if (!class_exists($class, false) &&
                !interface_exists($class, false))
            {
                throw new Exception('File '.$file.' was loaded but class '.$class.' was not found');
            }
        }
    }
    
        5
  •  2
  •   gradbot    16 年前
        6
  •  1
  •   Matthias Winkelmann    16 年前

        7
  •  1
  •   Ted    16 年前
        8
  •  1
  •   troelskn    16 年前