简体   繁体   English

我是否正确使用名称空间,自动加载和别名?

[英]Am I using namespaces, autoload, and aliasing correctly?

I've been reading a lot of posts on StackOverflow but I'm not really sure I'm using namespaces, autoloading, and aliasing correctly. 我已经阅读了很多关于StackOverflow的文章,但是我不确定我是否正确使用了名称空间,自动加载和别名。 This is functioning fine, but I'm not sure I'm properly using these concepts. 这可以正常工作,但是我不确定我是否正确使用了这些概念。 I've listed some reasons why I think this setup is incorrect at the bottom of the post. 我在帖子底部列出了一些我认为此设置不正确的原因。

Imagine the following directory structure: 想象一下以下目录结构:

\public_html
  - index.php
  \Classes
   \A
    - One.php
   \B
    - Two.php

One.php is structured like: One.php的结构如下:

<?php
namespace Classes\A;
class A { ....

Two.php is structured like: Two.php的结构如下:

<?php
namespace Classes\B;
class B { ....

Then, in index.php I do something like: 然后,在index.php中,我执行以下操作:

<?php
use Classes\A\One as One;
use Classes\B\Two as Two;

spl_autoload_register(function ($className) {
...
});

... ?>    

So, a couple things that bug me about this: 因此,有几件事使我感到困惑:

  1. If I am doing aliasing (the "use" statements) I still need to list out all of the files. 如果我在使用别名(“ use”语句),则仍然需要列出所有文件。 Aren't we trying to avoid doing this by using autoload? 我们不是要通过使用自动加载来避免这样做吗?

  2. If I want to use internal classes, I need to add a line such as "use \\mysqli;" 如果要使用内部类,则需要添加一行诸如“ use \\ mysqli;”。 into the class that uses this and do things like "new \\mysqli()". 进入使用该类的类,并执行“ new \\ mysqli()”之类的操作。 Seems kind of messy? 看起来有点凌乱?

  3. If a class extends a class from another namespace (say Two.php extends One.php for example) then I need to include "use \\Classes\\A\\One as One;" 如果一个类从另一个名称空间扩展了一个类(例如,Two.php扩展了One.php),那么我需要包括“使用\\ Classes \\ A \\ One作为One;”。 in One.php which seems to be what we want to avoid in the first place 在One.php中,这似乎是我们首先要避免的

  1. You don't have to reference all of your namespaced classes via a use statement. 您不必通过use语句引用所有命名空间的类。 You can use partial namespaces. 您可以使用部分名称空间。

     use Classes\\A; new A\\One(); 

    So you can see if you had another class in the A namespace it could be instantiated with 因此,您可以看到在A名称空间中是否有另一个类可以通过实例化

     new A\\Three(); 
  2. There are many things in the global namespace, you don't need to define them with use . 全局名称空间中有很多东西,您不需要use来定义它们。 You can just call them \\mysqli() . 您可以仅将它们称为\\mysqli() Yes it's a bit unsightly but it allows you to make a function called mysqli in your own code without worrying about collisions with globally namespaced functions. 是的,这有点难看,但是它允许您在自己的代码中创建一个名为mysqli的函数,而不必担心与全局命名空间函数的冲突。

  3. I'm not sure I follow you on this one. 我不确定我是否会关注您。 You don't need to do a use in the base class which references itself. 您不需要在引用自身的基类中use

Ultimately it kinda seems like you view use almost like include when they are very different, use , in this context, is a convenience thing so you don't have to type out full namespaces every time. 最终,看起来好像您在use几乎像include一样use ,在这种情况下use是一种方便的事情,因此您不必每次都键入完整的名称空间。

Your Autoloader also doesn't need to know about your namespaces. 您的自动加载器也不需要了解您的名称空间。 The autoloader just tells PHP how to load a class. 自动加载器只是告诉PHP如何加载类。 So if it sees a class come in with the name Classes\\A\\One you can make it look in the directory /Classes/A for a file called One.php and include it. 因此,如果看到带有名称Classes\\A\\One ,则可以在目录/Classes/A中查找名为One.php的文件并将其包括在内。 PHP will make sure that the class is allocated in the proper namespace. PHP将确保在正确的名称空间中分配该类。 The autoloader is just a convenience thing to keep you from having to do includes and requires in each of your PHP files. 自动加载器只是一个方便的事情,它使您不必在每个PHP文件中都包含和要求。

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

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