Graphical tutorial on using PHP namespace (with code)
这次给大家带来php命名空间使用图文教程(附代码),php命名空间使用的注意事项有哪些,下面就是实战案例,一起来看一下。
一:命名空间里的namespace关键字和NAMESPACE常量的运用
PHP支持两种抽象的访问当前命名空间内部元素的方法,NAMESPACE 魔术常量和namespace关键字。
常量NAMESPACE会储存当前命名空间的名字字符串,如果当前是全局非命名空间,则保存的是空字符串。
关键字 namespace 可用来显式访问当前命名空间或子命名空间中的元素。它等价于类中的 self 操作符。如果不在当前是全局环境的话,那么显式访问的就是全局限定的元素。
NAMESPACE实例:
<?php namespace Index\Name{ var_dump(NAMESPACE); //打印string(10) "Index\Name" function foo($classname){ return NAMESPACE.'\\'.$classname; } } namespace{ var_dump(NAMESPACE); //打印string(0) "" var_dump(Index\Name\foo('ceshi')); //打印string(16) "Index\Name\ceshi" } ?>
namespace实例:
<?php namespace Index\Name\Index{ function foo(){ return 2; } } namespace Index\Name{ var_dump(namespace\Index\foo()); //打印2 function foo(){ return 1; } } namespace{ var_dump(namespace\Index\Name\foo()); //打印1 } ?>
二:使用命名空间的别名/导入
命名空间具备允许别名导入以及导入的功能,命名空间导入需使用use关键字,如果还需要设置别名则需要use与as进行配合。
1)导入支持范围:
1:为类名称使用别名
2:为接口名称使用别名
3:为命名空间使用别名
4:5.6以上的php版本,允许函数或者常量使用别名。
2)别名/导入格式
导入格式:use [函数/常量] 命名空间完全限定名[类/接口/函数/常量]
别名导入格式:use [函数/常量] 命名空间完全限定名[类/接口/函数/常量] as 别名
注意:如果没有使用完全限定名的话,也和之前使用命名空间一样,会变成当前命名空间+限定名称来组合出完整的命名空间,所以如果不写完全限定名称,这里一定要多留意组合的结果是否是正确的命名空间。
导入实例:
<?php namespace Index\Col\Ads{ const INSTANCE='const_val'; function functionName(){ return 'function_val'; } class className{ static function classv(){ return 'class_val'; } } } namespace Col{ const INSTANCE='const_val_col'; function functionName(){ return 'function_val_col'; } class className{ static function classv(){ return 'class_val_col'; } } } namespace Index{ /*引入Index\Col\Ads命名空间*/ use \Index\Col\Ads; /*读取引入的命名空间的常量*/ echo \Index\Col\Ads\INSTANCE.'<br/>'; //打印class_val /*读取引入的命名空间的函数*/ echo \Index\Col\Ads\functionName().'<br>'; //打印class_val /*读取引入的命名空间的类,接口也是一样的*/ echo \Index\Col\Ads\className::classv().'<br>'; //打印class_val /*引入常量*/ use const \Col\INSTANCE; /*读取常量*/ echo INSTANCE.'<br>'; //打印const_val_col /*引入函数*/ use function \Col\functionName; /*读取函数*/ echo functionName().'<br>'; //打印function_val_col /*引入类或者接口*/ use \Col\className; /*读取类或者接口*/ echo className::classv().'<br>'; //打印classname_val_col } ?>
以上的例子里Index的命名空间里写的是完全限定名,Index\Col\Ads如果没有前面的\全局操作符的话,就会变成Index\Index\Col\Ads的命名空间了,一定要注意。
别名导入实例:
<?php namespace Index\Col\Ads{ const INSTANCE='const_val'; const NS='namespace'; function functionName(){ return 'function_val'; } class className{ static function classv(){ return 'class_val'; } } } namespace{ /*引入Index\Col\Ads命名空间,并设置别名Ads*/ use Index\Col\Ads as Ads; /*引入Index\Col\Ads命名空间的常量INSTANCE,并设置别名con*/ use const Index\Col\Ads\INSTANCE as con; /*引入Index\Col\Ads命名空间的函数functionName,并设置别名func*/ use function Index\Col\Ads\functionName as func; /*引入Index\Col\Ads命名空间的类className,并设置别名classn,接口的别名设置方式和这个一样*/ use Index\Col\Ads\className as classn; echo Ads\NS.'<br/>'; //打印namespace echo con.'<br>'; //打印const_val echo func().'<br>'; //打印function_val echo classn::classv().'<br>'; //打印class_val } ?>
这个例子里是全局的非命名空间,所以没有全局操作符也不会影响导入的命名空间。
三:特别补充
1:命名空间首字符不能是数字,必须是字母或者是下划线,否则会报出farse error。
2:define在命名空间内设置的常量默认是全局的(例外:一个文件内多个命名空间用括号包起来的方式define默认设置的是该命名空间的常量),所以如果需要命名空间下的常量,需要特别写明在常量名称里,例如define('Index\CON','CON')和define(NAMESPACE.'\CON','CON')这两种方式都是设置命名空间下的常量CON。
常量实例:
<?php namespace Col; /*define默认设置的是全局的常量*/ define('CON','globals'); /*特别声明设立的是当前命名空间下的常量*/ define(NAMESPACE.'\CON','col'); /*特别声明设立的是Index命名空间下的常量*/ define('Index\CON','index'); /*全局操作符后直接跟常量名,所以获取到的是全局的常量CON*/ var_dump(\CON); //globals /*没有任何限定,所以获取的是当前命名空间的常量CON*/ var_dump(CON); //col /*全局限定,读取其对应的Col命名空间的常量CON*/ var_dump(\Col\CON); //col /*全局限定,读取其对应的Index命名空间的常量CON*/ var_dump(\Index\CON); //index ?>
3:看到上面的实例,推及到函数及类(接口)就不一样了,在命名空间里设置的函数及类(接口)都是属于该命名空间的内容,不管是不是一个文件多个命名空间大括号里设置的。
函数和类的实例:可以看出在命名空间内的函数和类是属于命名空间的
index.php
<?php function foo(){ return 'global'; } class fool{ static function ceshi(){ return 'global'; } } ?>
col.php
<?php namespace Col; require './index.php'; //如果不引入index.php文件,那么下面的\foo()和\foo::ceshi()都会报fatal error function foo(){ return 1; } class fool{ static function ceshi(){ return 2; } } var_dump(\foo()); //global var_dump(foo()); //打印1 var_dump(\Col\foo()); //打印1 var_dump(\fool::ceshi()); //global var_dump(fool::ceshi()); //打印2 var_dump(\Col\fool::ceshi()); //打印2 ?>
4:设置命名空间的时候,要注意不要使用php的关键字,例如function、class、abstract之类的,否则会报出parse error。
5:同一个命名空间,不同文件间的使用无须带上命名空间,直接使用函数、常量、类及接口就可以了。
6:一个命名空间的类、常量、接口、函数单独引入另一个命名空间,其中函数、常量、类、接口如果发生了冲突,如果没有用限定词则优先使用单独引入类、常量、接口、函数。
实例:
indext.php
<?php namespace Lic; define(NAMESPACE.'\CON',1); function func(){ echo 1; } class foo{ static function ceshi(){ return 1; } }
只引入命名空间
<?php namespace Col; require './indext.php'; use \Lic; define(NAMESPACE.'\CON',2); //设定命名空间的常量必须写明命名空间,否则是全局的常量 function func(){ echo 2; } class foo{ static function ceshi(){ return 2; } } var_dump(CON); //打印2 var_dump(namespace\CON); //打印2 func(); //打印2 namespace\func(); //打印2 var_dump(foo::ceshi()); //打印2 var_dump(namespace\foo::ceshi()); //打印2
如果单独引入类、接口、函数、常量的情况,名称冲突且没有用限定的话优先使用引入的:
<?php namespace Col; require './indext.php'; use \Lic\foo; use function \Lic\func; use const \Lic\CON; define(NAMESPACE.'\CON',2); //设定命名空间的常量必须写明命名空间,否则是全局的常量 function func(){ echo 2; } class foo{ static function ceshi(){ return 2; } } var_dump(CON); //打印1 var_dump(namespace\CON); //打印2 func(); //打印1 namespace\func(); //打印2 var_dump(foo::ceshi()); //打印1 var_dump(namespace\foo::ceshi()); //打印2
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Graphical tutorial on using PHP namespace (with code). For more information, please follow other related articles on the PHP Chinese website!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!