且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导入软件包或自动加载PHP?

更新时间:2021-10-02 09:30:07

我广泛使用__autoload().我们在应用程序中使用的autload函数为旧类的向后兼容性进行了一些调整,但是在创建允许autoload()相当正常工作的新类时,我们通常遵循约定:

I use __autoload() extensively. The autload function that we use in our application has a few tweaks for backwards compatibility of older classes, but we generally follow a convention when creating new classes that allow the autoload() to work fairly seemlessly:

  • 一致的类命名:每个类在其自己的文件中,每个类都以驼峰式命名,并用下划线分隔.这映射到类路径.例如,Some_CoolClass映射到我们的类目录,然后映射到'Some/CoolClass.class.php'.我认为某些框架使用此约定.
  • 明确需要外部类:由于我们无法控制所使用的任何外部库的命名,因此我们使用PHP的require_once()函数加载它们.
  • Consistent Class Naming: each class in its own file, each class is named with camel-case separated by an underscore. This maps to the class path. For example, Some_CoolClass maps to our class directory then 'Some/CoolClass.class.php'. I think some frameworks use this convention.
  • Explicitly Require External Classes: since we don't have control over the naming of any external libraries that we use, we load them using PHP's require_once() function.