随笔 - 122,  文章 - 2,  评论 - 2,  阅读 - 54649

观察代码

//银联支付类调用初始化
  use Vendor\UnionPay\UnionPayUserV1; // 导入外部框架的类

    public function UoionPayInit() {
        $client = new \UnionPayV1();  // 这里\的作用是什么?
        $this->client=$client;
    }

函数测试

加 \ 可以调用指定命名空间下的函数, 如果只是单独的一个 \ 就是调用根命名空间下的函数。

namespace Test;
  function func(){
	echo 'namespace - Test';
  }
  namespace TXT;
  function func(){
	  echo 'namespace - TXT';
  }
  
  // \func(); //输出  Fatal error: Call to undefined function func() in D:\phpStudy\WWW\ppp.php on line 94
  // TXT\func(); //输出 Fatal error: Call to undefined function TXT\TXT\func() in D:\phpStudy\WWW\ppp.php
  // \TXT\func(); //输出 namespace - TXT
  \Test\func(); //输出 namespace - Test

测试类

利用PHP内置类进行测试 PHP Manualen

  <?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }
	
	/*public function Test(){
		$stream = new HttpInflateStream;
		echo $stream->update('String');
		echo $stream->finish(); //输出 Class 'Home\Controller\HttpInflateStream' not found
	}*/

  public function Test(){
		$stream = new \HttpInflateStream;
		echo $stream->update('String');
		echo $stream->finish();  //输出 Class 'HttpInflateStream' not found
	}
}

结论

和命名空间有关,因为所需要的银联里面没有用到命名空间,框架中启用了,会在当前命名空间中寻找这个类,找不到会报错。

posted on   depressiom  阅读(0)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示