Home >Backend Development >PHP Tutorial >php 命名空间的问题

php 命名空间的问题

WBOY
WBOYOriginal
2016-06-06 20:30:511045browse

namespace php;

class demo
{

}

$s = 'demo';

new demo; //正常

new $s; // Class 'demo' not found in 这个啥情况

回复内容:

namespace php;

class demo
{

}

$s = 'demo';

new demo; //正常

new $s; // Class 'demo' not found in 这个啥情况

<code>$s = 'php\demo';
$obj = new $s;

</code>

new demo;因为之前申明了namespace,所以代码都是在本namespace,也就是\php这个空间。所以可以直接new 本空间的class。
而 new $s;是在\ 根空间 执行的,所以要带上'\php\demo'或者 'php\demo'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn