[英]Php require() does not recognize aliased class
So mainly this is caused by my code structure: 所以这主要是由我的代码结构引起的:
File1.php File1.php
use \Class1 as Blah;
require 'File2.php';
File2.php File2.php
$closure = function ($arg) {
return new Blah($arg);
};
Somehow the part behind ... as
isn't recognized after using require()
. 不知何故
... as
使用require()
后无法识别的部分。
Namespace aliases are only valid within the file in which you write the use
statement. 命名空间别名仅在您编写
use
语句的文件内有效。 The aliases do not transfer across file boundaries. 别名不会跨文件边界传输。 If you want to
use Class1 as Blah
within File2.php, you need to put that statement at the top of File2.php. 如果要在File2.php中将
use Class1 as Blah
,则需要将该语句放在File2.php的顶部。
is not recognized by the code - Slim is not a compiler, it is a framework.
无法被代码识别 -Slim不是编译器,而是框架。
The code will execute to the PHP compiler standards, therefore using the default. 该代码将按照PHP编译器标准执行,因此使用默认值。
use SomeNamespace\SomeClass as SomePrefix;
Will work no matter what you're trying to achieve. 无论您要实现什么目标,它都将起作用。 The
require_once()
method loads your PHP file holding the class, which I assume doesn't have a custom namespace since you're targeting the \\
directory. require_once()
方法加载包含该类的PHP文件,由于您的目标是\\
目录,因此我认为该类没有自定义名称空间。
If you're working inside a class then this might be your issue, your code should run similar to this after using the require_once()
to load in the ParseMarkdownClass
file. 如果在类内部工作,则可能是问题所在,使用
require_once()
加载到ParseMarkdownClass
文件中后,代码应与此类ParseMarkdownClass
。
use ParseMarkdownClass as Md;
class SomeClass
{
public $container;
public function __construct()
{
$this->container = (object)['m' => function() {
return new Md();
}];
}
}
(new SomeClass())->$container->m()->...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.