仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 732|回复: 8
打印 上一主题 下一主题

[学习教程] PHP教程之PHP面向工具中的主要常识点(三)

[复制链接]
山那边是海 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:13:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
理解动态语言的概念,运做机制,熟悉PHP语法1.namespace:和C++中的名字空间很像,感化也一样,都是为了不在援用较多第三方库时而带来的名字抵触成绩。经由过程名字空间,即使两个class的称号不异,可是由于位于分歧的名字空间内,他们仍旧能够被准确定位和辨别。第一次看到PHP的名字空间语法时,感到和C++比拟在语法上长短常十分类似的,但是在写点儿小例子做做实行的时分才发明,他们的不同仍是很年夜的,为了不今后健忘,以是这里出格将其纪录了上去。见以下代码:复制代码<?php//inTest2.phpnamespacenstest        est2;classTest2{publicstaticfunctionprintMe(){printThisisnstest        est2Test2::printSelf.."
";}}<?php//inTest1.phpnamespacenstest        est1;classTest1{publicstaticfunctionprintMe(){printThisisnstest        est1Test1::printSelf.."
";}}require"Test2.php";nstest        est2Test2::printMe();复制代码运转了局以下:bogon:TestPhp$phpTest1.phpPHPFatalerror:Classnstest        est1
stest        est2Test2notfoundin/Users/liulei/PhpstormProjects/TestPhp/Test1.phponline13是否是这个了局对照出人意料,缘故原由在哪呢?HOHO,本来PHP在举行名字空间援用的时分,假如名字空间的第一个字符不是前导斜杠(),那末就被主动辨认为绝对名字空间,在下面的代码中,Test1本身地点的名字空间是namespacenstest        est1,因而在以nstest        est2Test2::printMe()体例挪用Test2::printMe()时,PHP将主动剖析为nstest        est1
stest        est2Test2::printMe(),即以为nstest        est2是在以后名字空间外部的。修改该成绩十分复杂,只需在援用时加上前导斜杠()便可,见以下修复后的代码:复制代码<?php//Test2.phpnamespacenstest        est2;classTest2{publicstaticfunctionprintMe(){printThisisnstest        est2Test2::printSelf.."
";}}<?php//Test1.phpnamespacenstest        est1;classTest1{publicstaticfunctionprintMe(){printThisisnstest        est1Test1::printSelf.."
";}}require"Test2.php";
stest        est2Test2::printMe();复制代码运转了局以下:bogon:TestPhp$phpTest1.phpThisisnstest        est2Test2::printSelf.另有一种修改体例,能够表示一下PHP中名字空间中的绝对援用。这里我们能够将Test1的名字空间改成namespacenstest,其他的修正见以下代码中白色高亮部分:复制代码<?php//Test2.phpnamespacenstest        est2;classTest2{publicstaticfunctionprintMe(){printThisisnstest        est2Test2::printSelf.."
";}}<?php//Test1.phpnamespacenstest;classTest1{publicstaticfunctionprintMe(){printThisisnstest        est1Test1::printSelf.."
";}}require"Test2.php";test2Test2::printMe();复制代码运转了局即是下面准确的了局。最主要的不同就是该例利用了PHP名字空间中的绝对定位。信任熟习C++的开辟者必定会想到use关头字,PHP也供应了该关头字,他们的功效是分歧的,都是为了不在前面的代码中,无需再经由过程全限制符(类名前加名字空间前缀)来援用其他名字空间中的类了。至于详细的语律例则,仍是看看上面详细的代码和关头性正文吧。复制代码<?php//Test2.phpnamespacenstest        est2;classTest2{publicstaticfunctionprintMe(){printThisisnstest        est2Test2::printSelf.."
";}}<?php//Test1.phpnamespacenstest        est1;classTest1{publicstaticfunctionprintMe(){printThisisnstest        est1Test1::printSelf.."
";}}require"Test2.php";//这里必要出格注重的是,nstest        est2已暗示名字空间相对路径定位,不必要再加前导斜杠()了。//别的这里另有一个隐式划定规矩是test2暗示该名字空间的缺省别号,在援用其名字空间内的工具时必要加test2前缀。usenstest        est2;test2Test2::printMe();//这里我们也能够给名字空间显式的指定别号,如:usenstest        est2astest2_alias;test2_aliasTest2::printMe();复制代码运转了局以下:bogon:TestPhp$phpTest1.phpThisisnstest        est2Test2::printSelf.Thisisnstest        est2Test2::printSelf.最初先容一下PHP中全局名字空间的援用体例,见以下代码和关头性正文:复制代码<?phpclassTest{publicstaticfunctionprintMe(){printThisisGlobalnamespaceTest::printSelf.."
";}}//上面两行代码暗示的是统一工具,即全局名字空间下的Test类,但是假如由于名字空间抵触招致第一种体例不克不及被PHP//编译器一般辨认,那末就能够利用第二种体例显式的关照PHP,本人要援用的是全局名字空间中的Test类。Test::printMe();Test::printMe();复制代码运转了局以下:bogon:TestPhp$phpTest1.phpThisisGlobalnamespaceTest::printSelf.ThisisGlobalnamespaceTest::printSelf.2.Reflection:PHP中的反射和Java中java.lang.reflect包供应的功效一样,更成心思的是,就连良多办法定名和挪用体例也长短常相同的。他们都是由一些列能够剖析类、类办法和办法参数的PHP内置类构成。我们这里次要先容的是以下几个经常使用的内置类:(Reflection、RelectionClass、ReflectionMethod、ReflectionParameter和ReflectionProperty)。如今我们仍是一步一步来了解,即从ReflectionClass入手下手给出示例代码和关头性正文:复制代码<?phpclassTestClass{public$publicVariable;functionpublicMethod(){print"ThisispublicMethod.
";}}functionclassInfo(ReflectionClass$c){$details="";//getName将前往实践的类名。$name=$c->getName();if($c->isUserDefined()){$details.="$nameisuserdefined.
";}if($c->isInternal()){$details.="$nameisbuilt-in.
";}if($c->isAbstract()){$details.="$nameisabstractclass.
";}if($c->isFinal()){$details.="$nameisfinalclass.
";}if($c->isInstantiable()){$details.="$namecanbeinstantiated.
";}else{$details.="$namecannotbeinstantiated.
";}return$details;}functionclassSource(ReflectionClass$c){$path=$c->getFileName();$lines=@file($path);//猎取类界说代码的肇端行和停止行。$from=$c->getStartLine();$to=$c->getEndLine();$len=$to-$from+1;returnimplode(array_slice($lines,$from-1,$len));}print"ThefollowingisClassInformation.
";printclassInfo(newReflectionClass(TestClass));print"
ThefollowingisClassSource.
";printclassSource(newReflectionClass(TestClass));复制代码运转了局以下:复制代码bogon:TestPhp$phpreflection_test.phpThefollowingisClassInformation.TestClassisuserdefined.TestClasscanbeinstantiated.ThefollowingisClassSource.classTestClass{public$publicVariable;functionpublicMethod(){print"ThisispublicMethod.
";}}复制代码上面让我们仍旧以代码示例和关头性正文的办法持续ReflectionMethod的进修之旅。复制代码<?phpclassTestClass{public$publicVariable;function__construct(){}privatefunctionprivateMethod(){}functionpublicMethod(){print"ThisispublicMethod.
";}functionpublicMethod2(string$arg1,int$arg2){}}//这个函数中利用的ReflectionMethod中的办法都长短常复杂直不雅的,就不再过量赘述了。functionmethodInfo(ReflectionMethod$m){$name=$m->getName();$details="";if($m->isUserDefined()){$details.="$nameisuserdefined.
";}if($m->isInternal()){$details.="$nameisbuilt-in.
";}if($m->isAbstract()){$details.="$nameisabstract.
";}if($m->isPublic()){$details.="$nameispublic.
";}if($m->isProtected()){$details.="$nameisprotected.
";}if($m->isPrivate()){$details.="$nameisprivate.
";}if($m->isStatic()){$details.="$nameisstatic.
";}if($m->isFinal()){$details.="$nameisfinal.
";}if($m->isConstructor()){$details.="$nameisconstructor.
";}if($m->returnsReference()){$details.="$namereturnsareference.
";}return$details;}functionmethodSource(ReflectionMethod$m){$path=$m->getFileName();$lines=@file($path);$from=$m->getStartLine();$to=$m->getEndLine();$len=$to-$from+1;returnimplode(array_slice($lines,$from-1,$len));}$rc=newReflectionClass(TestClass);$methods=$rc->getMethods();print"Thefollowingismethodinformation.
";foreach($methodsas$method){printmethodInfo($method);print"
--------------------
";}print"ThefollowingisMethod[TestClass::publicMethod]source.
";printmethodSource($rc->getMethod(publicMethod));复制代码运转了局以下:复制代码bogon:TestPhp$phpreflection_test.phpThefollowingismethodinformation.__constructisuserdefined.__constructispublic.__constructisconstructor.--------------------privateMethodisuserdefined.privateMethodisprivate.--------------------publicMethodisuserdefined.publicMethodispublic.--------------------publicMethod2isuserdefined.publicMethod2ispublic.--------------------ThefollowingisMethod[TestClass::publicMethod]source.functionpublicMethod(){print"ThisispublicMethod.
";}复制代码让我们持续ReflectionParameter吧,他暗示的是成员函数的参数信息。持续看代码吧。复制代码<?phpclassParamClass{}classTestClass{functionpublicMethod(){print"ThisispublicMethod.
";}functionpublicMethod2(ParamClass$arg1,&$arg2,$arg3=null){}}functionparamInfo(ReflectionParameter$p){$details="";//这里的$declaringClass将即是TestClass。$declaringClass=$p->getDeclaringClass();$name=$p->getName();$class=$p->getClass();$position=$p->getPosition();$details.="$$namehasposition$position.
";if(!empty($class)){$classname=$class->getName();$details.="$$namemustbea$classnameobject
";}if($p->isPassedByReference()){$details.="$$nameispassedbyreference.
";}if($p->isDefaultValueAvailable()){$def=$p->getDefaultValue();$details.="$$namehasdefault:$def
";}return$details;}$rc=newReflectionClass(TestClass);$method=$rc->getMethod(publicMethod2);$params=$method->getParameters();foreach($paramsas$p){printparamInfo($p)."
";}复制代码运转了局以下:复制代码bogon:TestPhp$phpreflection_test.php$arg1hasposition0.$arg1mustbeaParamClassobject$arg2hasposition1.$arg2ispassedbyreference.$arg3hasposition2.$arg3hasdefault:复制代码下面先容的都是经由过程PHP供应的ReflectionAPI来遍历恣意class的详细信息,现实上和Java等其他言语供应的反射功效一样,PHP也一样撑持经由过程反射类挪用实践工具的办法,这里将次要使用到两个办法,分离是ReflectionClass::newInstance()来创立工具实例,另外一个是ReflectionMethod::invoke(),依据工具实例和办法名实行该办法。见以下代码:复制代码<?phpclassTestClass{private$privateArg;function__construct($arg){$this->privateArg=$arg;}functionpublicMethod(){print$privateArg=.$this->privateArg."
";}functionpublicMethod2($arg1,$arg2){print$arg1=.$arg1.$arg2=.$arg2."
";}}$rc=newReflectionClass(TestClass);$testObj=$rc->newInstanceArgs(array(Thisisprivateargument.));$method=$rc->getMethod(publicMethod);$method->invoke($testObj);$method2=$rc->getMethod(publicMethod2);$method2->invoke($testObj,"hello","world");复制代码运转了局以下:bogon:TestPhp$phpreflection_test.php$privateArg=Thisisprivateargument.$arg1=hello$arg2=world现实上ReflectionClass、ReflectionMethod和ReflectionParameter供应给我们的可用办法另有更多,这里只是给出几个最典范的办法,以便我们能够更加直不雅的进修和懂得PHPReflectionAPI。信任再看完今后的代码示例以后,我们城市对照分明,假如从此必要用到和class相干的功效,就从ReflectionClass中查找,而memberfunction的信息则必定来自于ReflectionMethod,办法参数信息来自于ReflectionParameter。注:该Blog中纪录的常识点,是在我进修PHP的过程当中,碰到的一些PHP和其他面向工具言语比拟对照共同的中央,大概是对我自己而言的确必要簿记上去以备后查的常识点。固然谈不上甚么深度,可是仍是但愿能与人人分享。HTML中的任何元素都要亲自实践,只有明白了什么元素会起到什么效果之后,你才会记忆深刻,而一味的啃书,绝对是不行的,我想大部分新手之所以觉得概念难学,大部分是一个字“懒”,懒是阻止进步的最大敌人,所以克服掉懒的习惯,才能更快的学好一样工具。
分手快乐 该用户已被删除
沙发
发表于 2015-1-19 05:08:36 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
若相依 该用户已被删除
板凳
发表于 2015-1-24 16:02:19 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
乐观 该用户已被删除
地板
发表于 2015-2-2 10:47:15 | 只看该作者
爱上php,他也会爱上你。
活着的死人 该用户已被删除
5#
发表于 2015-2-7 18:22:29 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
小魔女 该用户已被删除
6#
发表于 2015-2-22 22:42:31 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
金色的骷髅 该用户已被删除
7#
发表于 2015-3-7 04:09:06 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
再现理想 该用户已被删除
8#
发表于 2015-3-14 11:43:01 | 只看该作者
php是动态网站开发的优秀语言,在学习的时候万万不能冒进。在系统的学习前,我认为不应该只是追求实现某种效果,因为即使你复制他人的代码调试成功,实现了你所期望的效果,你也不了解其中的原理。
因胸联盟 该用户已被删除
9#
发表于 2015-3-21 05:30:31 | 只看该作者
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-20 16:49

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表