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

php 命名空间的问题

WBOY
Release: 2016-06-06 20:30:51
Original
1049 people have browsed it

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>
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template