命名空间在 PHP "inherited" 中吗?
Are namespaces in PHP "inherited"?
编辑:
是的,问题是在 use
语句的开头使用了 \。正如 M1ke 指出的那样,use
从根元素开始。
原版post
我认为是一个 PHP 问题,但它可能是 Drupal。
我正在开发一个无头 Drupal 项目,该项目使用 class(我称之为实体模型),该项目使用名为 EntityFieldQuery 的 Drupal class。
在创建或使用此 class 我 bootstrap Drupal 使用之前:
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
实体模型 class 在模型名称 space 中,如下所示:
namespace Models;
use \EntityFieldQuery;
class EntityModel
{
.....
$query = new EntityFieldQuery();
$query->doSomething();
......
}
EntityFieldQuery 在我使用“\”时完美地找到了,因为这个 class 不在模型名称中space。
问题是创建此 class 时使用了其他 class 未使用任何名称的 space,并且出现以下错误:
class Models\InsertQuery not found in ....
这里是使用InsertQuery
的EntityFieldQuery使用的class
class InsertQuery_mysql extends InsertQuery ...
我不明白为什么找到 InsertQuery_mysql 但 InsertQuery
我最终在 InsertQuery 中添加了一个“\”来解决问题,如下所示:
class InsertQuery_mysql extends \InsertQuery ...
实际上这个 class 在一个名为 query.inc 的 php 文件中,它包含两个定义 classes(在同一个文件中,我不知道这是一个也有问题)
class InsertQuery_mysql extends InsertQuery
....
class TruncateQuery_mysql extends TruncateQuery
我想如果我使用 "new \ClassName()" 这个 class 里面的 "default namespace" 也会是 "\" 而不是第一个叫 class 的名字space.
我不喜欢修改第 3 方库,有什么办法可以避免这种情况吗?我猜是架构问题而不是缺乏定义如果有人有更好的主意,我很感激。
谢谢!
EDIT2:添加更多信息...
按执行顺序。
index.php:
require_once 'vendor/autoload.php';
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
...
app/SiteController.php:
use Models\Campaign;
class SiteController {
...
$campaing = new Campaign();
...
app/Models/Campaing.php:
namespace Models;
class Campaign extends EntityModel {
...
app/Models/EntityModel.php:
namespace Models;
use \EntityFieldQuery; //<-- this should go without \ as I say in EDIT section
class EntityModel {
...
public function getAll() {
$query = new EntityFieldQuery(); //<--throwed Models\InsertQuery not found. It must have \ at the beginning of the class name.
回答基本问题(和未决的进一步代码)PHP 命名空间由文件中声明的任何命名空间设置。
// Bar.php
namespace Foo;
class Bar {}
// some other file
use Foo\Bar;
$test = new Bar(); //works
// different file
namespace Foo;
$test = new Bar(); // works
// another file
require 'Bar.php';
// won't work because we are not in namespace "Foo"
$test = new Bar();
在您的特定情况下,use \EntityLoader
应该是 use EntityLoader
,因为您正在退出您想要进入的命名空间。
编辑:
是的,问题是在 use
语句的开头使用了 \。正如 M1ke 指出的那样,use
从根元素开始。
原版post
我认为是一个 PHP 问题,但它可能是 Drupal。
我正在开发一个无头 Drupal 项目,该项目使用 class(我称之为实体模型),该项目使用名为 EntityFieldQuery 的 Drupal class。
在创建或使用此 class 我 bootstrap Drupal 使用之前:
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
实体模型 class 在模型名称 space 中,如下所示:
namespace Models;
use \EntityFieldQuery;
class EntityModel
{
.....
$query = new EntityFieldQuery();
$query->doSomething();
......
}
EntityFieldQuery 在我使用“\”时完美地找到了,因为这个 class 不在模型名称中space。
问题是创建此 class 时使用了其他 class 未使用任何名称的 space,并且出现以下错误:
class Models\InsertQuery not found in ....
这里是使用InsertQuery
的EntityFieldQuery使用的classclass InsertQuery_mysql extends InsertQuery ...
我不明白为什么找到 InsertQuery_mysql 但 InsertQuery
我最终在 InsertQuery 中添加了一个“\”来解决问题,如下所示:
class InsertQuery_mysql extends \InsertQuery ...
实际上这个 class 在一个名为 query.inc 的 php 文件中,它包含两个定义 classes(在同一个文件中,我不知道这是一个也有问题)
class InsertQuery_mysql extends InsertQuery
....
class TruncateQuery_mysql extends TruncateQuery
我想如果我使用 "new \ClassName()" 这个 class 里面的 "default namespace" 也会是 "\" 而不是第一个叫 class 的名字space.
我不喜欢修改第 3 方库,有什么办法可以避免这种情况吗?我猜是架构问题而不是缺乏定义如果有人有更好的主意,我很感激。
谢谢!
EDIT2:添加更多信息...
按执行顺序。
index.php:
require_once 'vendor/autoload.php';
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
...
app/SiteController.php:
use Models\Campaign;
class SiteController {
...
$campaing = new Campaign();
...
app/Models/Campaing.php:
namespace Models;
class Campaign extends EntityModel {
...
app/Models/EntityModel.php:
namespace Models;
use \EntityFieldQuery; //<-- this should go without \ as I say in EDIT section
class EntityModel {
...
public function getAll() {
$query = new EntityFieldQuery(); //<--throwed Models\InsertQuery not found. It must have \ at the beginning of the class name.
回答基本问题(和未决的进一步代码)PHP 命名空间由文件中声明的任何命名空间设置。
// Bar.php
namespace Foo;
class Bar {}
// some other file
use Foo\Bar;
$test = new Bar(); //works
// different file
namespace Foo;
$test = new Bar(); // works
// another file
require 'Bar.php';
// won't work because we are not in namespace "Foo"
$test = new Bar();
在您的特定情况下,use \EntityLoader
应该是 use EntityLoader
,因为您正在退出您想要进入的命名空间。