Home > Backend Development > PHP Tutorial > 一个PHP命名空间的问题很无语的问题.

一个PHP命名空间的问题很无语的问题.

WBOY
Release: 2016-06-23 13:48:23
Original
1047 people have browsed it

  直接上点小代码


<?php//用来定义一个继承Smarty的子类,在构造函数中实现基本的路径配置.....namespace Smarty;class Smarty_Test  {    function setName(){    	echo 'Name';    }}
Copy after login


<?phpuse Smarty\Smarty_Test;$smarty = new Smarty_Test();$smarty->assign('name','Ned');$smarty->debugging = true;$smarty->display('index.tpl');?>
Copy after login


代码如上,很简单,两段代码的文件结构在同一个目录下

就这样,运行死活说找不到类..........
Fatal error: Class 'Smarty\Smarty_Test' not found in ............
实在是没搞明白,我在ThinkPHP框架里也是使用命名空间完全没问题,怎么新建一个项目放连个文件,测试一下它死活都不行呢....连ZEND STUDIO编辑器都能显示出有存在Smarty_Test这个类,怎么运行的时候就是不行,,郁闷啊


回复讨论(解决方案)

没人吗..............

很不错LLL

你没有加载含有 Smarty_Test 类定义的文件

你没有加载含有 Smarty_Test 类定义的文件



如何加载?使用include包含进去是吗?

是的,include包含进来
或者设置自动加载

spl_autoload_register();use Smarty\Smarty_Test; $smarty = new Smarty_Test();$smarty->assign('name','Ned');$smarty->debugging = true;$smarty->display('index.tpl');
Copy after login

Related labels:
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