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

php 命名空间的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:30:51
Original
1086 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
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template