но композитор автоматически загружает один и тот же базовый класс в той же папке.
Моя ошибка: Неустранимая ошибка: класс 'VendorName\ParentFolder\Enums\BasicEnum не найден в C:\VendorName\www\src\ParentFolder\Enums\MyEnumeration.php в строке 5.
MyEnumeration.php:
<?php
namespace VendorName\ParentFolder\Enums;
abstract class MyEnumeration extends BasicEnum {
const ConstantOne = 1;
const ConstantTwo = 2;
const ConstantThree = 3;
}
и BasicEnum.php:
<?php
namespace VendorName\ParentFolder;
abstract class BasicEnum {
private static $constCacheArray = NULL;
private function __construct() { }
private static function getConstants() {
if (self::$constCacheArray == NULL) {
self::$constCacheArray = [];
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value, $strict = true) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict);
}
}
Моя структура папок:
+ VendorName
+ www
+ src
+ ParentFolder
+ Enums
MyEnumeration.php
BasicEnum2.php
И автозагрузка построена через:
composer dump-autoload
с композитором.json:
{
"autoload": {
"psr-4": { "VendorName\\": "src/"}
}
}
Моя .php-страница:
<h1>Composer Autoload Test</h1>
<p>Trying to load a class where base class is in parent folder.
<p>(Using the PHP.net BasicEnum example).
<br/>
<?php
require __DIR__ . '/vendor/autoload.php';
use VendorName\ParentFolder;
use VendorName\ParentFolder\Enums;
echo '<br/><br/>';
if (class_exists('MyEnumeration')) {
echo 'MyEnumeration exists';
}
else {
echo 'MyEnumeration does NOT exist'; // This line prints out in browser.
}
echo '<br/><br/>';
echo '<br/><br/>';
if (class_exists('VendorName\ParentFolder\Enums\MyEnumeration')) { // This line blows up because BaseEnum is not found in MyEnumeration.php.
echo 'VendorName\ParentFolder\Enums\MyEnumeration exists';
}
else {
echo 'VendorName\ParentFolder\Enums\MyEnumeration does NOT exist';
}
echo '<br/><br/>';
echo '<br/><br/>';
if (defined('VendorName\ParentFolder\Enums\MyEnumeration::ConstantOne')) {
echo 'VendorName\ParentFolder\Enums\MyEnumeration::ConstantOne exists';
}
else {
echo 'VendorName\ParentFolder\Enums\MyEnumeration::ConstantOne does NOT exist';
}
echo '<br/><br/>';
echo 'NotExist:' . (MyEnumeration::isValidName('NotExist') ? 'true' : 'false') . '<br/>';
echo 'ConstantOne:' . (MyEnumeration::isValidName('ConstantOne') ? 'true' : 'false') . '<br/>';
echo 'ConstantTwo:' . (MyEnumeration::isValidName('ConstantTwo') ? 'true' : 'false') . '<br/>';
echo 'ConstantThree:' . (MyEnumeration::isValidName('ConstantThree') ? 'true' : 'false') . '<br/>';
?>
Наконец, я должен полностью квалифицировать MyEnumeration с помощью пространства имен, иначе оно не будет найдено, даже если у меня есть оператор «использовать» и использовать автозагрузку композитора.
composer.json
? - person Matt Gibson   schedule 19.09.2017