1. namespace:
和C++中的名字空间很像,作用也一样,都是为了避免在引用较多第三方库时而带来的名字冲突问题。通过名字空间,即便两个class的名称相同,但是因为位于不同的名字空间内,他们仍然可以被精确定位和区分。第一次看到PHP的名字空间语法时,感觉和C++相比在语法上是非常非常相似的,然而在写点儿小例子做做实验的时候才发现,他们的差别还是很大的,为了避免以后忘记,所以这里特别将其记录了下来。见如下代码:
复制代码
//in Test2.php
namespace nstest\test2;
class Test2 {
public static function printMe() {
print 'This is nstest\test2\Test2::printSelf.'."\n";
}
}
//in Test1.php
namespace nstest\test1;
class Test1 {
public static function printMe() {
print 'This is nstest\test1\Test1::printSelf.'."\n";
}
}
require "Test2.php";
nstest\test2\Test2::printMe();
复制代码
运行结果如下:
bogon:TestPhp$ php Test1.php
PHP Fatal error: Class 'nstest\test1\nstest\test2\Test2' not found in /Users/liulei/PhpstormProjects/TestPhp/Test1.php on line 13
是不是这个结果比较出乎意料,原因在哪呢?HOHO,原来PHP在进行名字空间引用的时候,如果名字空间的第一个字符不是前导斜杠(\),那么就被自动识别为相对名字空间,在上面的代码中,Test1自身所在的名字空间是namespace nstest\test1,因此在以nstest\test2\Test2::printMe()方式调用Test2::printMe()时,PHP将自动解析为nstest\test1\nstest\test2\Test2::printMe(),即认为nstest\test2是在当前名字空间内部的。修正该问题非常简单,只需在引用时加上前导斜杠(\)即可,见以下修复后的代码:
复制代码
//Test2.php
namespace nstest\test2;
class Test2 {
public static function printMe() {
print 'This is nstest\test2\Test2::printSelf.'."\n";
}
}
//Test1.php
namespace nstest\test1;
class Test1 {
public static function printMe() {
print 'This is nstest\test1\Test1::printSelf.'."\n";
}
}
require "Test2.php";
\nstest\test2\Test2::printMe();
复制代码
运行结果如下:
bogon:TestPhp$ php Test1.php
This is nstest\test2\Test2::printSelf.
还有一种改动方式,可以示意一下PHP中名字空间中的相对引用。这里我们可以将Test1的名字空间改为namespace nstest,其他的修改见以下代码中红色高亮部分:
复制代码
//Test2.php
namespace nstest\test2;
class Test2 {
public static function printMe() {
print 'This is nstest\test2\Test2::printSelf.'."\n";
}
}
//Test1.php
namespace nstest;
class Test1 {
public static function printMe() {
print 'This is nstest\test1\Test1::printSelf.'."\n";
}
}
require "Test2.php";
test2\Test2::printMe();
复制代码
运行结果等于上面正确的结果。最重要的差别就是该例使用了PHP名字空间中的相对定位。相信熟悉C++的开发者一定会想到use关键字,PHP也提供了该关键字,他们的功能是一致的,都是为了避免在后面的代码中,无需再通过全限定符(类名前加名字空间前缀)来引用其他名字空间中的类了。至于具体的语法规则,还是看看下面具体的代码和关键性注释吧。
复制代码
//Test2.php
namespace nstest\test2;
class Test2 {
public static function printMe() {
print 'This is nstest\test2\Test2::printSelf.'."\n";
}
}
//Test1.php
namespace nstest\test1;
class Test1 {
public static function printMe() {
print 'This is nstest\test1\Test1::printSelf.'."\n";
}
}
require "Test2.php";
//这里需要特别注意的是,nstest\test2已经表示名字空间绝对路径定位,不需要再加前导斜杠(\)了。
//另外这里还有一个隐式规则是test2表示该名字空间的缺省别名,在引用其名字空间内的对象时需要加test2前缀。
use nstest\test2;
test2\Test2::printMe();
//这里我们也可以给名字空间显式的指定别名,如:
use nstest\test2 as test2_alias;
test2_alias\Test2::printMe();
复制代码
运行结果如下:
bogon:TestPhp$ php Test1.php
This is nstest\test2\Test2::printSelf.
This is nstest\test2\Test2::printSelf.
最后介绍一下PHP中全局名字空间的引用方式,见如下代码和关键性注释:
复制代码
class Test {
public static function printMe() {
print 'This is Global namespace Test::printSelf.'."\n";
}
}
//下面两行代码表示的是同一对象,即全局名字空间下的Test类,然而如果因为名字空间冲突导致第一种方式不能被PHP
//编译器正常识别,那么就可以使用第二种方式显式的通知PHP,自己要引用的是全局名字空间中的Test类。
Test::printMe();
\Test::printMe();
复制代码
运行结果如下:
bogon:TestPhp$ php Test1.php
This is Global namespace Test::printSelf.
This is Global namespace Test::printSelf.
2. Reflection:
PHP中的反射和Java中java.lang.reflect包提供的功能一样,更有意思的是,就连很多方法命名和调用方式也是非常雷同的。他们都是由一些列可以分析类、类方法和方法参数的PHP内置类组成。我们这里主要介绍的是如下几个常用的内置类:(Reflection、RelectionClass、ReflectionMethod、ReflectionParameter和ReflectionProperty)。现在我们还是一步一步来理解,即从ReflectionClass开始给出示例代码和关键性注释:
复制代码
class TestClass {
public $publicVariable;
function publicMethod() {
print "This is publicMethod.\n";
}
}
function classInfo(ReflectionClass $c) {
$details = "";
//getName将返回实际的类名。
$name = $c->getName();
if ($c->isUserDefined()) {
$details .= "$name is user defined.\n";
}
if ($c->isInternal()) {
$details .= "$name is built-in.\n";
}
if ($c->isAbstract()) {
$details .= "$name is abstract class.\n";
}
if ($c->isFinal()) {
$details .= "$name is final class.\n";
}
if ($c->isInstantiable()) {
$details .= "$name can be instantiated.\n";
} else {
$details .= "$name cannot be instantiated.\n";
}
return $details;
}
function classSource(ReflectionClass $c) {
$path = $c->getFileName();
$lines = @file($path);
//获取类定义代码的起始行和截至行。
$from = $c->getStartLine();
$to = $c->getEndLine();
$len = $to - $from + 1;
return implode(array_slice($lines,$from - 1,$len));
}
print "The following is Class Information.\n";
print classInfo(new ReflectionClass('TestClass'));
print "\nThe following is Class Source.\n";
print classSource(new ReflectionClass('TestClass'));
复制代码
运行结果如下:
复制代码
bogon:TestPhp$ php reflection_test.php
The following is Class Information.
TestClass is user defined.
TestClass can be instantiated.
The following is Class Source.
class TestClass {
public $publicVariable;
function publicMethod() {
print "This is publicMethod.\n";
}
}
复制代码
下面让我们仍然以代码示例和关键性注释的方法继续ReflectionMethod的学习之旅。
复制代码
class TestClass {
public $publicVariable;
function __construct() {
}
private function privateMethod() {
}
function publicMethod() {
print "This is publicMethod.\n";
}
function publicMethod2(string $arg1, int $arg2) {
}
}
//这个函数中使用的ReflectionMethod中的方法都是非常简单直观的,就不再过多赘述了。
function methodInfo(ReflectionMethod $m) {
$name = $m->getName();
$details = "";
if ($m->isUserDefined()) {
$details .= "$name is user defined.\n";
}
if ($m->isInternal()) {
$details .= "$name is built-in.\n";
}
if ($m->isAbstract()) {
$details .= "$name is abstract.\n";
}
if ($m->isPublic()) {
$details .= "$name is public.\n";
}
if ($m->isProtected()) {
$details .= "$name is protected.\n";
}
if ($m->isPrivate()) {
$details .= "$name is private.\n";
}
if ($m->isStatic()) {
$details .= "$name is static.\n";
}
if ($m->isFinal()) {
$details .= "$name is final.\n";
}
if ($m->isConstructor()) {
$details .= "$name is constructor.\n";
}
if ($m->returnsReference()) {
$details .= "$name returns a reference.\n";
}
return $details;
}
function methodSource(ReflectionMethod $m) {
$path = $m->getFileName();
$lines = @file($path);
$from = $m->getStartLine();
$to = $m->getEndLine();
$len = $to - $from + 1;
return implode(array_slice($lines, $from - 1, $len));
}
$rc = new ReflectionClass('TestClass');
$methods = $rc->getMethods();
print "The following is method information.\n";
foreach ($methods as $method) {
print methodInfo($method);
print "\n--------------------\n";
}
print "The following is Method[TestClass::publicMethod] source.\n";
print methodSource($rc->getMethod('publicMethod'));
复制代码
运行结果如下:
复制代码
bogon:TestPhp$ php reflection_test.php
The following is method information.
__construct is user defined.
__construct is public.
__construct is constructor.
--------------------
privateMethod is user defined.
privateMethod is private.
--------------------
publicMethod is user defined.
publicMethod is public.
--------------------
publicMethod2 is user defined.
publicMethod2 is public.
--------------------
The following is Method[TestClass::publicMethod] source.
function publicMethod() {
print "This is publicMethod.\n";
}
复制代码
让我们继续ReflectionParameter吧,他表示的是成员函数的参数信息。继续看代码吧。
复制代码
class ParamClass {
}
class TestClass {
function publicMethod() {
print "This is publicMethod.\n";
}
function publicMethod2(ParamClass $arg1, &$arg2, $arg3 = null) {
}
}
function paramInfo(ReflectionParameter $p) {
$details = "";
//这里的$declaringClass将等于TestClass。
$declaringClass = $p->getDeclaringClass();
$name = $p->getName();
$class = $p->getClass();
$position = $p->getPosition();
$details .= "\$$name has position $position.\n";
if (!empty($class)) {
$classname = $class->getName();
$details .= "\$$name must be a $classname object\n";
}
if ($p->isPassedByReference()) {
$details .= "\$$name is passed by reference.\n";
}
if ($p->isDefaultValueAvailable()) {
$def = $p->getDefaultValue();
$details .= "\$$name has default: $def\n";
}
return $details;
}
$rc = new ReflectionClass('TestClass');
$method = $rc->getMethod('publicMethod2');
$params = $method->getParameters();
foreach ($params as $p) {
print paramInfo($p)."\n";
}
复制代码
运行结果如下:
复制代码
bogon:TestPhp$ php reflection_test.php
$arg1 has position 0.
$arg1 must be a ParamClass object
$arg2 has position 1.
$arg2 is passed by reference.
$arg3 has position 2.
$arg3 has default:
复制代码
上面介绍的都是通过PHP提供的Reflection API来遍历任意class的具体信息,事实上和Java等其他语言提供的反射功能一样,PHP也同样支持通过反射类调用实际对象的方法,这里将主要应用到两个方法,分别是ReflectionClass::newInstance()来创建对象实例,另一个是ReflectionMethod::invoke(),根据对象实例和方法名执行该方法。见如下代码:
复制代码
class TestClass {
private $privateArg;
function __construct($arg) {
$this->privateArg = $arg;
}
function publicMethod() {
print '$privateArg = '.$this->privateArg."\n";
}
function publicMethod2($arg1, $arg2) {
print '$arg1 = '.$arg1.' $arg2 = '.$arg2."\n";
}
}
$rc = new ReflectionClass('TestClass');
$testObj = $rc->newInstanceArgs(array('This is private argument.'));
$method = $rc->getMethod('publicMethod');
$method->invoke($testObj);
$method2 = $rc->getMethod('publicMethod2');
$method2->invoke($testObj,"hello","world");
复制代码
运行结果如下:
bogon:TestPhp$ php reflection_test.php
$privateArg = This is private argument.
$arg1 = hello $arg2 = world
事实上ReflectionClass、ReflectionMethod和ReflectionParameter提供给我们的可用方法还有更多,这里只是给出几个最典型的方法,以便我们可以更为直观的学习和了解PHP Reflection API。相信再看完以后的代码示例之后,我们都会比较清楚,如果今后需要用到和class相关的功能,就从ReflectionClass中查找,而member function的信息则一定来自于ReflectionMethod,方法参数信息来自于ReflectionParameter。
注:该Blog中记录的知识点,是在我学习PHP的过程中,遇到的一些PHP和其他面向对象语言相比比较独特的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但是还是希望能与大家分享。

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

在iOS17中,Apple为其移动操作系统引入了几项新的隐私和安全功能,其中之一是能够要求对Safari中的隐私浏览选项卡进行二次身份验证。以下是它的工作原理以及如何将其关闭。在运行iOS17或iPadOS17的iPhone或iPad上,如果您在Safari浏览器中打开了任何“无痕浏览”标签页,然后退出会话或App,Apple的浏览器现在需要面容ID/触控ID认证或密码才能再次访问它们。换句话说,如果有人在解锁您的iPhone或iPad时拿到了它,他们仍然无法在不知道您的密码的情况下查看您的隐私


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具