


An important new feature of PHP 5.3 is namespace.
This feature was proposed in PHP5.0x, but was later canceled and scheduled to be implemented in PHP6. This time, PHP 5.3 was released "ahead of schedule" again, which shows that developers attach great importance to it and are cautious.
The content of the document may be out of date when it is officially released (documentation maybe out dated), so here is a brief explanation of the usage of namespace: first, declare a namespace, add the new keyword namespace, and It should be at the beginning of the class file
<ol class="dp-c"> <li class="alt"><span><span><?php </SPAN></span><li class=""><span>namespace Project::Module; </span></li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> User { </span> </li></span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">const</font></strong></span><span> STATUS_OK = true; </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> register(</span><span class="vars"><font color="#dd0000">$data</font></span><span>) { </span> </li> <li class=""><span>... </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>... </span></li> <li class=""><span>} </span></li> </ol>
and then in the controller (maybe other files) you can call it like this
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$user</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Project::Module::User(); </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span> </li> </ol>
It is indeed no different from usual, but we can Connect two independent classes. For example,
<ol class="dp-c"> <li class="alt"><span><span>Project::Module::User; </span></span></li> <li class=""><span>Project::Module::Blog; </span></li> </ol>
makes it easier to describe and understand the relationship between variables and classes from the language itself, thereby avoiding the "traditional" lengthy naming method of Project_Module_Blog.
The above description may be difficult to explain the benefits of using namespaces. The newly added use and as keywords may explain the problem better. Use and as statements can reference and declare namespace "aliases". For example, the code for instantiating the class in the above controller can be written like this
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module; </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Module::User(); </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span> </li> </ol>
Even the constants in the
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module::User </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> ModuleUser; </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> ModuleUser; </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$user</font></span><span>->register(</span><span class="vars"><font color="#dd0000">$register_info</font></span><span>); </span> </li> </ol>
class can also be accessed through the namespace, such as in the above class STATUS_OK can be accessed through the namespace
<ol class="dp-c"><li class="alt"><span><span>Project::Module::User::STATUS_OK </span></span></li></ol>
. Furthermore, you can also use aliases to simplify such long "variable names"
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">use</font></strong></span><span> Project::Module::User::STATUS_OK </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> STATUS_OK; </span></span></li> <li class=""> <span></span><span class="func">echo</span><span> STATUS_OK; </span> </li> </ol>
By the way, mention the concept of "Hyperspace (The Global Namespace)". The so-called "hyperspace" refers to variables, classes and functions that do not have a designated namespace. For example, functions like
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> foo() { </span></span></li> <li class=""><span>... </span></li> <li class="alt"><span>} </span></li> </ol>
can be executed using foo() or ::foo();.
Finally, use the autoload function to load the class in the specified namespace. A simple function is as follows:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> __autoload( </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ) { </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$classname</font></span><span> = </span><span class="func">strtolower</span><span>( </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ); </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$classname</font></span><span> = </span><span class="func">str_replace</span><span>( </span><span class="string"><font color="#0000ff">'::'</font></span><span>, DIRECTORY_SEPARATOR, </span><span class="vars"><font color="#dd0000">$classname</font></span><span> ); </span> </li> <li class=""> <span></span><span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>( dirname( </span><span class="keyword"><strong><font color="#006699">__FILE__</font></strong></span><span> ) . </span><span class="string"><font color="#0000ff">'/'</font></span><span> . </span><span class="vars"><font color="#dd0000">$classname</font></span><span> . </span><span class="string"><font color="#0000ff">'.class.php'</font></span><span> ); </span> </li> <li class="alt"><span>} </span></li> </ol>
. For example, calling
<ol class="dp-c"><li class="alt"><span><span>__autoload(</span><span class="string"><font color="#0000ff">'Project::Module::User'</font></span><span>); </span></span></li></ol>
can automatically load the Project_Module_User.class.php file (although this seems inconvenient).

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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