Heim  >  Artikel  >  Backend-Entwicklung  >  php中的use有什么作用

php中的use有什么作用

PHPz
PHPzOriginal
2016-06-06 20:38:109025Durchsuche

php中的use的作用:引入命名空间下的文件,语法为:【use app\admin\model\LiveRecharge;】。use与【require_once】和【include】是不相同的,使用use的前提是文件已经包含进当前文件。

php中的use有什么作用

php中use的作用是什么?

PHP中的use、命名空间的理解

看.Net中的命名空间和using

using Ddd.Core;
using Ddd.Core.Caching;
using Ddd.Core.Data;
using Ddd.Core.Domain.Customers;
using Ddd.Core.Domain.QiNiu;
using Ddd.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ddd.Services.QiNiu

看Java中的命名空间和import

package demo.jq.com.relativelayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

看PHP中的命名空间和use

namespace app\admin\model;
use think\Model;
use think\Db;

综上可以看出,命名空间就是相当于一个文件夹的路径。同一个命名空间下,不能有相同的类文件。同名的类文件可以存在于不同的命名空间下面。

用了命名空间,可以方便的调用其他文件夹下的文件。

只需要通过using、import、use引入命名空间下的文件即可。

然后就可以在方法中新建对象了。

use app\admin\model\LiveRecharge;
$LiveRecharge = new LiveRecharge();

看.Net或者Java,可以看出using、import可以引入一些系统自带的类文件。

模块用到哪个,引入相应的系统类即可。

Thinkphp5中使用了命名空间的机制,表面上看麻烦了,每个类都需要写命名空间,其实是方便了。

有了命名空间,能够更便捷的使用各个模块的类,可以方便的引入外部模块,可以方便的使用其他模块的Model,或者一些通用的类引入都便捷了。

让PHP更加的面向对象。

注意:use不等于require_once或者include,use的前提是已经把文件包含进当前文件。

更多相关技术知识,请访问PHP中文网

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn