如何在 Zend Framework 3 中自动加载继承的学说实体?
How to autoload inherited doctrine entity in Zend Framework 3?
<?php
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/**
* @Entity
*/
class Employee extends Person
{
// ...
}
如何为上述学说实体继承定义Autoload?或者我们可以将每个实体放在单独的文件中,例如 Entity\Person.php , Employee\Person.php ?
如果您希望您的项目与(例如)PSR-4 (Autoloader).
兼容,则应将每个 PHP class 放入其自己的文件中
引用自 PSR-4 规范:
When loading a file that corresponds to a fully qualified class name …
- A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a “namespace prefix”) corresponds to at least one “base directory”.
- The contiguous sub-namespace names after the “namespace prefix” correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
- The terminating class name corresponds to a file name ending in
.php
. The file name MUST match the case of the terminating class name.
因此,如果您在 MyProject\Model
命名空间中创建 class Person
,自动加载器将只能在 …/Model/Person.php
文件中找到它。 Employee
class 必须转到单独的 …/Model/Employee.php
文件才能供自动加载器使用。
<?php
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/**
* @Entity
*/
class Employee extends Person
{
// ...
}
如何为上述学说实体继承定义Autoload?或者我们可以将每个实体放在单独的文件中,例如 Entity\Person.php , Employee\Person.php ?
如果您希望您的项目与(例如)PSR-4 (Autoloader).
兼容,则应将每个 PHP class 放入其自己的文件中引用自 PSR-4 规范:
When loading a file that corresponds to a fully qualified class name …
- A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a “namespace prefix”) corresponds to at least one “base directory”.
- The contiguous sub-namespace names after the “namespace prefix” correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
- The terminating class name corresponds to a file name ending in
.php
. The file name MUST match the case of the terminating class name.
因此,如果您在 MyProject\Model
命名空间中创建 class Person
,自动加载器将只能在 …/Model/Person.php
文件中找到它。 Employee
class 必须转到单独的 …/Model/Employee.php
文件才能供自动加载器使用。