简体   繁体   English

Phalcon:名称空间类声明错误

[英]Phalcon: namespace class declaration error

My folder structure is: 我的文件夹结构是:

/app/config/loader.php
/app/validation/Domain.php

Domain.php Domain.php

namespace Validation
{
    use Phalcon\Validation\Validator as Validator;

    class Email extends Validator
    {
        public function validate($validator, $attribute)
        {
            $result = parent::validate($validator,$attribute);

            return $result;
        }
    }
}

Loader.php Loader.php

$loader = new \Phalcon\Loader();

$loader->registerNamespaces(
    array(
        'Validation'  => __DIR__ . '/../validation/'
    )
);

$loader->register();

echo '<pre><br/>';
var_dump(new Validation\Email());
die();

and it returns an error: 它返回一个错误:

Fatal error: Class 'Validation\\Domain\\Email' not found 致命错误:找不到类“ Validation \\ Domain \\ Email”

What i'm doing wrong? 我做错了什么?

PS: An example here http://docs.phalconphp.com/en/latest/reference/loader.html also does't work. PS:此处的示例http://docs.phalconphp.com/en/latest/reference/loader.html也不起作用。

// Creates the autoloader
$loader = new \Phalcon\Loader();

//Register some namespaces
$loader->registerNamespaces(
    array(
       "Example\Base"    => "vendor/example/base/",
       "Example\Adapter" => "vendor/example/adapter/",
       "Example"         => "vendor/example/",
    )
);

// register autoloader
$loader->register();

// The required class will automatically include the
// file vendor/example/adapter/Some.php
$some = new Example\Adapter\Some();

I created the same structure and I get an error: 我创建了相同的结构,但出现错误:

Fatal error: Class 'Example\\Adapter\\Some' not found 致命错误:找不到类“ Example \\ Adapter \\ Some”

Phalcon loader has some questionable reputation. Phalcon装载机的声誉令人怀疑。 If you use composer it would often make more sense to use that for autoloading your own code via the autoload directive. 如果您使用composer,通常会更有意义地使用它通过autoload指令自动加载自己的代码。

# composer.json
{

    "require": {
        "phpunit/dbunit": "*",
        "phpunit/phpunit": "*",
        "…": "…"
    },
    "autoload": {
        "psr-0": {
            "": "../src"
        }
    }
}

Otherwise the issue would be with the paths, make sure you take into account subfolders (where you config sits, where the loaded code sits, etc). 否则,问题将出在路径上,请确保考虑到子文件夹(配置所在的位置,加载的代码所在的位置等)。 It seems you need to change 'app/validation/' to '../../app/validation/' – figure this out yourself. 看来您需要将'app/validation/'更改为'../../app/validation/'自己弄清楚。 Also try setting the absolute path if that doesn't do the job using __DIR__ . '../../app/validation/' 如果使用__DIR__ . '../../app/validation/'不能完成此工作,也请尝试设置绝对路径__DIR__ . '../../app/validation/' __DIR__ . '../../app/validation/' . __DIR__ . '../../app/validation/'

Edit: 编辑:

In your structure you say you have Domain.php but loading Domain\\Email – surely the problem is here. 在您的结构中,您说您具有Domain.php但正在加载Domain\\Email –当然问题出在这里。 I also tested it locally, the only instance when the loader works is when the absolute path is provided (and given you actually have app/validator/Domain/Email.php file with a class). 我也在本地进行了测试,只有当提供了绝对路径时,加载程序才能正常工作(并且您实际上拥有带有类的app / validator / Domain / Email.php文件)。

$loader->registerNamespaces(
    array(
        'Validation'  => __DIR__ . '/../../app/validation/'
    )
);

The advice on the composer autoloader stays up. 关于作曲家自动装带器的建议保持不变。 Note how you are not using PSR-0 standard for your namespaces ( validator starts with a lower letter, your Validator namespace starts with a capital), which isn't cool… 请注意,您如何不对名称空间使用PSR-0标准( validator以小写字母开头, Validator命名空间以大写字母开头),这并不酷……

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

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