简体   繁体   English

在每个页面上使用不带use关键字的spl_autoload吗?

[英]Using spl_autoload without use keyword on every page?

We have recently been rewriting our PHP application's model code using OO. 我们最近一直在使用OO重写PHP应用程序的模型代码。 We have the classes done, and we included namespacing (in the form of Project\\Level1\\Level2 or whatever). 我们完成了所有的类,并包含了命名空间(以Project \\ Level1 \\ Level2或其他任何形式)。 Since this is an already-built application, there are a lot of classes. 由于这是一个已构建的应用程序,因此有很多类。 We are using an autoloader, for ease of loading the classes, which is great. 我们正在使用自动加载器,以便于加载类,这很棒。 But here's where the hassle comes in. 但这就是麻烦的来源。

We have probably 300 pages strewn all across the application, and all of them require a PHP file at the very beginning (a sort of bootstrap script). 我们可能在整个应用程序中散布了300个页面,并且所有页面一开始都需要一个PHP文件(一种引导脚本)。 The autoloader script is required in that bootstrap file. 该引导程序文件中需要自动装带器脚本。

Now, most of our pages will be using at least one but likely many of the classes. 现在,我们的大多数页面将使用至少一个,但可能使用许多类。 We know that we have two options here: 我们知道这里有两个选择:

//either call the classes using qualified names
$person = new Project\Person;

//or include the "use" keyword on every page, so we can alias the classes for ease of use
use Project\Person as Person;
$person = new Person;

But the hassle is that we would really rather not have to do this on every single page in our application: 但麻烦的是,我们确实宁愿不必在应用程序的每个页面上都执行此操作:

require_once('../php/bootstrap.php');
use Project\Person\ as Person,
    Project\Address\ as Address,
    Project\Group\ as Group
    Project\CustomField\ as CustomField;
$person = new Person;
$address = new Address;

We tried, for the heck of it, including the use keyword for all our classes in the bootstrap file, but according to the PHP manual, "Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules." 我们尝试过在引导文件中包含所有类的use关键字,但是根据PHP手册, “导入规则是基于文件的,这意味着所包含的文件将不会继承父文件的导入规则。”

It just seems like including these sprawling use statements on every page of our application is creating more hassle than it's worth. 似乎在我们的应用程序的每一页上包含这些庞大的use语句正在造成比其价值更大的麻烦。 And what if we are using 15 classes on one page? 如果我们在一页上使用15个类怎么办? Then the use statement would be huge. 那么use语句将非常庞大。

My question is this: is it too much hassle? 我的问题是:麻烦太多了吗? Are we correct in using namespaces, or is it not all that helpful in PHP? 我们在使用名称空间时是否正确,还是在PHP中没有那么有用? And now knowing what we are trying to do, are we doing it right? 现在知道了我们要做什么,我们做对了吗? Is there something we are missing? 我们缺少什么吗? Should we be calling classes with fully qualified names all the time or something? 我们是否应该一直用完全限定的名称来调用类?

(this is my first question on stackoverflow, so please let me know if I need to do anything differently or better with my questions in the future) (这是我关于stackoverflow的第一个问题,所以请让我知道以后是否需要对我的问题做其他更改或更好的操作)

While using vanilla Zend Framework Autoloader might not be they way you want to go, the Zend Loader does support custom autoloaders, that can respect namespaces specified in-line or dynamically at run-time. 虽然使用香草Zend Framework Autoloader可能不是您想要的方式,但Zend Loader确实支持自定义自动加载器,这些自动加载器可以遵循以行方式指定或在运行时动态指定的名称空间。 In your case, you are using use Project\\Person\\ as Person which can become cumbersome, as you already noticed. 就您而言,您已经将use Project\\Person\\ as Person ,这已经很麻烦了,您已经注意到了。

Using namespaces with the autoloader will allow you to take advantage of auto loading using simple path traversal to organize classes, without having to append large paths just to get to the interesting parts. 将名称空间与自动加载器一起使用将使您能够利用通过简单路径遍历来自动分类的优势来组织类,而不必为获取有趣的部分而附加大路径。

So, instead of new Corp_Assets_Generic_Person_Address , you could keep the folder structure and use new Namespace_Person_Address . 因此,您可以保留文件夹结构并使用new Namespace_Person_Address来代替new Corp_Assets_Generic_Person_Address

Namespaces avoid clashes in class names. 命名空间避免类名冲突。 Specially in large applications implementing autoloaders that need to load the right class from a number of different libraries. 特别是在实现自动加载器的大型应用程序中,需要从许多不同的库中加载正确的类。 Using shorter class names makes the code easier to read but may make it harder to maintain. 使用较短的类名可以使代码更易于阅读,但可能难以维护。 Adding the use statements makes it easier to read and later maintain but then it would be cumbersome to write and refactor. 添加use语句使它更易于阅读和以后维护,但是编写和重构将变得很麻烦。 Documenting the expected behavior of the autoloader and using custom autoloaders to make more readable class names would be my suggestion. 我建议记录自动加载器的预期行为,并使用自定义自动加载器使类名更具可读性。

在php.ini中,将您的文件添加为prepend_file

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

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