带有句点的目录会破坏基于 Zend Framework 中命名空间的自动加载解析吗?

发布于 2024-10-15 17:43:56 字数 239 浏览 3 评论 0原文

我的库文件夹中有一个以我的网站命名的文件夹。文件夹路径类似于:

~\www\library\myWebsite.com

如果我使用 Zend 自动加载器加载库路径中所有内容的命名空间,那么从该文件中自动加载具有如下命名空间的类时是否会遇到任何问题:

\myWebsite.com\myClass::myFunction();

我在网上查找了这方面的文档,但找不到任何有关以这种方式使用句点的信息。

I have a folder in my library folder which is named after my website. The folder path is like:

~\www\library\myWebsite.com

If I'm using Zend autoloader to load the namespace of everything in the library path, will I have any trouble autoloading a class from that file with a namespace like this:

\myWebsite.com\myClass::myFunction();

I have looked online for documentation on this and I can't find any info about using periods in this way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-10-22 17:43:56

我试过了,复杂的是PHP。我认为 Zend 正在注册命名空间,因为当我调用 \Zend_Load_Autoloader::getRegisteredNamespaces() 时,它显示它已注册。但是当我从完全限定的命名空间调用静态方法时,php 给出了这样的错误:

Fatal error: Undefined constant 'myWebsite' in /home/jesse/www/application/controllers/MyController.php on line 15 

似乎 PHP 在解析期间正在终止命名空间标识符。 (句点字符)。这令人失望,因为对我来说,拥有一个以网站命名的库对我的设计很重要。

我会将目录重命名为 myWebsitecom,或者可能将 .com 设为自己的子目录,如

myWebsite\com 并将其合并到我的命名空间树中,如: \MyNamespace\Com\MyClass::myFunction();

I tried it and the complication is in PHP. I think Zend is registering the namespace fine, because when I call \Zend_Load_Autoloader::getRegisteredNamespaces() it shows that it's registered. but when I call the static method from the fully qualified namespace, php gives an error of this:

Fatal error: Undefined constant 'myWebsite' in /home/jesse/www/application/controllers/MyController.php on line 15 

It seems like PHP is terminating the namespace identifier, during parsing, at the . (period character). This is dissapointing because to me having a library named after the website was important to my design.

I will rename the directory to myWebsitecom or possibly make the .com it's own sub directory like

myWebsite\com and incorporate that into my namespace tree like: \MyNamespace\Com\MyClass::myFunction();

超可爱的懒熊 2024-10-22 17:43:56

找出答案的最简单方法就是尝试一下。

如果它不起作用,您始终可以编写一个自定义自动加载器来使其起作用。我对 php 命名空间没有太多经验,但是自动加载器看起来像这样(我想你必须稍微修改一下才能确定给定类名的正确文件路径):

<?php
class My_Loader_Autoloader_MyWebsite implements Zend_Loader_Autoloader_Interface {

    /**
     * (non-PHPdoc)
     * @see Zend_Loader_Autoloader_Interface::autoload()
     */
    public function autoload($class) {

        if (strtolower(substr($class, 0, 9)) == 'mywebsite') {
            $file = realpath(APPLICATION_PATH . '/../library/myWebsite.com/' . $class);
            if ($file) {
                require_once $file;
                return $class;
            }
        }
        return false;
    }
}

然后将其放入引导程序中:

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(new My_Loader_Autoloader_MyWebsite());

如果这个类必须位于 myWebsite.com 目录中,您也可以作弊并在其中添加一个 require :

 require_once(APPLICATION_PATH . '/../library/myWebsite.com/Loader/Autoloader/MyWebsite.php');

The easiest way to find out is to try it.

If it doesn't work, you could always write a custom autoloader to make it work. I don't have much experience with php namespaces, but the autoloader would look something like this (I imagine you'll have to tinker with it a bit to determine the correct file path given the class name):

<?php
class My_Loader_Autoloader_MyWebsite implements Zend_Loader_Autoloader_Interface {

    /**
     * (non-PHPdoc)
     * @see Zend_Loader_Autoloader_Interface::autoload()
     */
    public function autoload($class) {

        if (strtolower(substr($class, 0, 9)) == 'mywebsite') {
            $file = realpath(APPLICATION_PATH . '/../library/myWebsite.com/' . $class);
            if ($file) {
                require_once $file;
                return $class;
            }
        }
        return false;
    }
}

then put this in your bootstrap:

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(new My_Loader_Autoloader_MyWebsite());

and if this class must be in that myWebsite.com directory, you could just cheat and throw in a require in there too:

 require_once(APPLICATION_PATH . '/../library/myWebsite.com/Loader/Autoloader/MyWebsite.php');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文