Composer 自动加载未在父文件夹中加载基础 class
Composer autoload not loading base class in parent folder
但是,composer 确实会在同一文件夹中自动加载相同的基类。
我的错误:
致命错误:Class 'VendorName\ParentFolder\Enums\BasicEnum not found in C:\VendorName\www\src\ParentFolder\Enums\MyEnumeration.php on line 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
与 composer.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,否则即使我有 'use' 语句并使用 composer autoload 也找不到它。
看来你有
文件src\ParentFolder\Enums\BasicEnum2.php
with class BasicEnum
在命名空间 VendorName\ParentFolder
但你必须有文件 src\ParentFolder\Enums\BasicEnum.php
(没有 2)
并且在你的 MyEnumeration.php
脚本中你必须有这个 use 块:
use VendorName\ParentFolder\BasicEnum;
您的 MyEnumeration
class 在命名空间 VendorName\ParentFolder\Enums
中。因此,它无法找到 BasicEnum
作为上述命名空间 \VendorName\ParentFolder
中的符号。您可以在 child class:
中绝对指定它
abstract class MyEnumeration extends \VendorName\ParentFolder\BasicEnum {
您还需要在 BasicEnum 中为 ReflectionClass
指明全局命名空间:
$reflect = new \ReflectionClass($calledClass);
另请注意,您 can't just import everything from a namespace 使用 use
。您需要 use
特定的 classes。例如,在您的 php 页面中,指定:
use VendorName\ParentFolder\Enums\MyEnumeration;
...允许在没有限定符的情况下使用 MyEnumeration
。
但是,composer 确实会在同一文件夹中自动加载相同的基类。
我的错误: 致命错误:Class 'VendorName\ParentFolder\Enums\BasicEnum not found in C:\VendorName\www\src\ParentFolder\Enums\MyEnumeration.php on line 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
与 composer.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,否则即使我有 'use' 语句并使用 composer autoload 也找不到它。
看来你有
文件src\ParentFolder\Enums\BasicEnum2.php
with class BasicEnum
在命名空间 VendorName\ParentFolder
但你必须有文件 src\ParentFolder\Enums\BasicEnum.php
(没有 2)
并且在你的 MyEnumeration.php
脚本中你必须有这个 use 块:
use VendorName\ParentFolder\BasicEnum;
您的 MyEnumeration
class 在命名空间 VendorName\ParentFolder\Enums
中。因此,它无法找到 BasicEnum
作为上述命名空间 \VendorName\ParentFolder
中的符号。您可以在 child class:
abstract class MyEnumeration extends \VendorName\ParentFolder\BasicEnum {
您还需要在 BasicEnum 中为 ReflectionClass
指明全局命名空间:
$reflect = new \ReflectionClass($calledClass);
另请注意,您 can't just import everything from a namespace 使用 use
。您需要 use
特定的 classes。例如,在您的 php 页面中,指定:
use VendorName\ParentFolder\Enums\MyEnumeration;
...允许在没有限定符的情况下使用 MyEnumeration
。