Reputation: 3218
My folder structure is:
/app/config/loader.php
/app/validation/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 = 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
What i'm doing wrong?
PS: An example here http://docs.phalconphp.com/en/latest/reference/loader.html also does't work.
// 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
Upvotes: 1
Views: 1722
Reputation: 9075
Phalcon loader has some questionable reputation. If you use composer it would often make more sense to use that for autoloading your own code via the autoload
directive.
# 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. Also try setting the absolute path if that doesn't do the job using __DIR__ . '../../app/validation/'
.
Edit:
In your structure you say you have Domain.php
but loading Domain\Email
– surely the problem is here. 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).
$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…
Upvotes: 2