如何使用命名空间 import/use static class

How to import/use static class with namespace

我已经在这几天了。似乎是世界上最简单的事情,但对于我来说,我无法让它发挥作用。

我正在尝试在我的控制器中使用这个 class:

https://github.com/neitanod/forceutf8

class 被命名为 Encoding.php,它有一个命名空间 ForceUTF8。

我已经尝试了以下所有方法:

App::uses('Encoding','Vendor'); //(copying Encoding.php directly in the Vendor dir)
App::uses('Encoding','Vendor/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
App::uses('Encoding’,’Lib'); //(copying Encoding.php directly in the Lib dir)
App::uses('Encoding’,’Lib/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
require_once(APP . 'Vendor' . DS . 'Encoding.php'); //(use plain old php require_once to load class from Vendor dir)

无论我做什么,我都会得到同样的错误:Class 'Encoding' 找不到。

我发现 CakePHP 文档在这个主题上非常混乱。一方面它说 not 对非 CakePHP classes 使用 App::uses 因为 classes 可能不遵循 CakePHP 标准。很公平。所以他们说使用 App::import。但是然后有很多 post 说 App::import 只不过是 require_once.

的包装器

所以在无法让 App::usesApp::import 工作之后,我尝试了 require_once。同样的错误。然后我在这里找到了一个 post 这么说即使在使用 require_once 时,你仍然必须 'initialize' 和 class 以便 CakePHP 能够 see/use它。那是怎么做到的? App::uses。所以我回到了起点。

有些东西告诉我名称空间导致了问题。当我使用 require_once 时 class 发现(我很确定)因为,例如,如果我改变

require_once(APP . 'Vendor' . DS . 'Encoding.php');

require_once(APP . 'Vendor' . DS . 'blabla.php');

它给我一个错误,找不到文件。

但是即使 require_once 似乎找到了,CakePHP 仍然说 class 没有找到。

如何加载带命名空间的供应商文件?

Cake2.x 不支持命名空间所以你需要自己写加载器

spl_autoload_register(function ($class) {
    foreach (App::path('Vendor') as $base) {
        $path = $base . str_replace('\', DS, $class) . '.php';
        if (file_exists($path)) {
            include $path;
            return;
        }
    }
});

更多信息在这里:

http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/

基于这篇文章: http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/comment-page-1

我正在使用贝宝SDK。这是我的代码。

在bootstrap.php中:

// Add 'vendors' as sub-package of 'Vendor' to the application
App::build(array('Vendor/vendors' => array('%s' . 'vendors' . DS)), App::REGISTER);
// Autoload the classes in the 'vendors'
spl_autoload_register(function ($class) {
    foreach (App::path('Vendor/vendors') as $base) {
        $path = $base . str_replace('\', DS, $class) . '.php';
        if (file_exists($path)) {
            include $path;
            return;
        }
    }
});

在 PaypalComponent 中:

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

在函数中使用那些类:

$this->apiContext = new ApiContext(
        new OAuthTokenCredential()
);

在 Cakephp 2.x 中,按照以下步骤操作

在 class 文件顶部添加

use \Dropbox as dbx;

[Dropbox 是我的供应商目录]

 use \Dropbox as dbx;

App::build(array('Vendor/Dropbox' => array('%s' . 'Dropbox' . DS)), App::REGISTER);
    // Autoload the classes in the 'vendors'
    spl_autoload_register(function ($class) {            
        foreach (App::path('Vendor') as $base) {               
            $path = $base . str_replace('\', DS, $class) . '.php';
            if (file_exists($path)) {
                include $path;
                return;
            }
        }
    });

现在调用您的 class 方法。