gps定位网站源码分享php?网页gps定位源码

老铁们,大家好,相信还有很多朋友对于gps定位网站源码分享php和网页gps定位源码的相关问题不太懂,没关系,今天就由我来为大家分享分享gps定位网站源码分享php以及网页gps定位源码的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!

和Java一样PHP中也提供了一套完整的反射API,何为反射?以前我们是先写类,再在类中添加各种方法和属性,最后实例化一个类对象调用属性和方法。那有我们没有办法只通过这个实例对象获取到关于这个类的全部信息呢,包括有哪些方法和属性它们分别在多少行?答案就是反射。

几个常用的反射API的类

ReflectionClass

classPeople{\nprotected$name=’Foo’;\nprotected$age=18;\npublicfunction__construct($name,$age)\n{\n$this->name=$name;\n$this->age=$age;\n}\npublicfunctionfunc1()\n{\necho’fun1′;\n}\npublicfunctiontoString()\n{\necho”Name:$this->name,Age:$this->age”.PHP_EOL;\n}\n}\nclassStudentextendsPeople{\npublic$major;\npublicfunction__construct($name,$age,$major)\n{\n$this->major=$major;\nparent::__construct($name,$age);\n}\npublicfunctiontoString()\n{\necho”Name:$this->name,Age:$this->age,Major:$this->major”.PHP_EOL;\n}\n}\n$reflect=newReflectionClass(‘Student’);\n//也可以传一个实例对象\n//$reflect=newReflectionClass(newStudent(‘Jason’,21,’CS’));\nReflection::export($reflect);\n\n

输出结果如下:

Class[<user>classStudentextendsPeople]{\n@@/…/demo1.php18-31\n\n-Constants[0]{\n}\n\n-Staticproperties[0]{\n}\n\n-Staticmethods[0]{\n}\n\n-Properties[3]{\nProperty[<default>public$major]\nProperty[<default>protected$name]\nProperty[<default>protected$age]\n}\n\n-Methods[2]{\nMethod[<user,overwritesPeople,ctor>publicmethod__construct]{\n@@/…/demo1.php21-25\n\n-Parameters[3]{\nParameter1[<required>$age]\nParameter1(3){\n[“major”]=>\nstring(2)”CS”\n[“name”:protected]=>\nstring(5)”Jason”\n[“age”:protected]=>\nint(21)\n}\n

虽然var_dump和print_r是打印数据的利器,但对于类和函数,反射API提供了更高层次的功能。同时,ReflectionClass还提供其他有用的方法:

//获取构造函数\n$reflect->getConstructor();\nobject(ReflectionMethod)2(2){\n[“name”]=>\nstring(11)”__construct”\n[“class”]=>\nstring(7)”Student”\n}\n[1]=>\nobject(ReflectionMethod)4(2){\n[“name”]=>\nstring(5)”func1″\n[“class”]=>\nstring(6)”People”\n}\n}\n//get_class_methods(‘Student’)\narray(3){\n[0]=>\nstring(11)”__construct”\n[1]=>\nstring(8)”toString”\n[2]=>\nstring(5)”func1″\n}\n

从输出结果可以很明显看出,使用反射的方法输出的结果更详细,可以看到每个方法具体属于哪个类,而get_class_methods是能打印出方法名。

详细的可以看官方文档:https://www.php.net/manual/en/class.reflectionmethod.php

ReflectionParameter

PHP中,在声明类方法时我们可以限制参数中对象的类型,因此检查方法的参数变得很有必要,反射API提供了ReflectionParameter类来使用。和获得ReflectionMethod对象数组的方式一样,我们可以使用ReflectionMethod::getParameters()方法返回ReflectionParameter对象数组。

$method=$reflect->getMethod(‘__construct’);\narray(3){\n[0]=>\nobject(ReflectionParameter)4(1){\n[“name”]=>\nstring(3)”age”\n}\n[2]=>\nobject(ReflectionParameter)#5(1){\n[“name”]=>\nstring(5)”major”\n}\n}\n

Student构造函数接受三个参数,我们可以知道具体的参数名,通过反射,我们还可以知道参数是否可以按引用传递,参数类型和是否接收空值作为参数等。

上面是我最近对PHP的反射api的简单学习。

发现一个有趣的package

Github上有个项目叫Roave/BetterReflection,是这个包提供的方法,我们可以更方便的调用PHP的反射,现在已经有600+star,地址如下:https://github.com/Roave/BetterReflection

BetterReflection

现在我们来简单学习下,这个包如何使用。

1.安装

$composerrequireroave/better-reflection\n

2.使用

创建ReflectionClass

useRoave\\BetterReflection\\Reflection\\ReflectionClass;\n$classInfo=ReflectionClass::createFromName(Student::class);//通过类名创建\n//或者\n$classInfo=ReflectionClass::createFromInstance(newStudent(‘Daniel’,18,’Math’));//通过实例创建\n

上面的静态初始化方式就等于下面:

useRoave\\BetterReflection\\BetterReflection;\n$classInfo=(newBetterReflection)->classReflector()->reflect(Student::class);\n

我们查看静态构造器createFromName的实现,会发现也是先实例化一个BetterReflection再进行反射的:

publicstaticfunctioncreateFromName(string$className):self\n{\nreturn(newBetterReflection())->classReflector()->reflect($className);\n}\n

获取ReflectionMethod、ReflectionParameter和ReflectionProperty也是一样一样的:

useRoave\\BetterReflection\\Reflection\\ReflectionMethod;\nuseRoave\\BetterReflection\\Reflection\\ReflectionParameter;\nuseRoave\\BetterReflection\\Reflection\\ReflectionProperty;\n$methodInfo=ReflectionMethod::createFromName(Student::class,’toString’);\n$methodInfo=ReflectionMethod::createFromInstance($student,’toString’);\n…\n

3.BetterReflection中的SourceLocator

SourceLocator的作用是根据不同的源码类型(有通过字符串传来的类、composer自动加载的类、一个指定文件名的类等),得到LocatedSource对象的(用于AST),这个对象指向要反射的类,主要就以下几个方法:

classLocatedSource\n{\n/**@varstring*/\nprivate$source;\n/**@varstring|null*/\nprivate$filename;\n/**\n*@throwsInvalidArgumentException\n*@throwsInvalidFileLocation\n*/\npublicfunction__construct(string$source,?string$filename){…}\npublicfunctiongetSource():string{…}\npublicfunctiongetFileName():?string{…}\n/**\n*IsthelocatedsourceinPHPinternals?\n*/\npublicfunctionisInternal():bool{…}\npublicfunctiongetExtensionName():?string{…}\n/**\n*Isthelocatedsourceproducedbyeval()or\\function_create()?\n*/\npublicfunctionisEvaled():bool{…}\n}\n

针对不同的情况,比如单个文件、源码字符串、闭包等,又衍生出了多种SourceLocator:

多种SourceLocator

ComposerSourceLocator:这应该是最常用的了,它使用composer自带的autoload来定位SingleFileSourceLocator:通过指定文件名来定位StringSourceLocator:直接使用定义类的字符串AutoloadSourceLocator:使用PHP内置的autoloader来定位,默认会使用这个定位器EvaledCodeSourceLocator:用于eval()创建的代码PhpInternalSourceLocator:用于PHP原生代码AnonymousClassObjectSourceLocator:用于PHP中的匿名类ClosureSourceLocator:用于闭包AggregateSourceLocator:合并多个SourceLocatorFileIteratorSourceLocator:用于可迭代的SplFileInfo实例数组DirectoriesSourceLocator:用于文件夹的情况,包括子文件夹

如何使用这这些Locator?

1.使用composer的autoload加载

useRoave\\BetterReflection\\BetterReflection;\nuseRoave\\BetterReflection\\Reflector\\ClassReflector;\nuseRoave\\BetterReflection\\SourceLocator\\Type\\ComposerSourceLocator;\n$classLoader=require’vendor/autoload.php’;\n$astLocator=(newBetterReflection())->astLocator();\n$reflector=newClassReflector(newComposerSourceLocator($classLoader,$astLocator));\n$reflectionClass=$reflector->reflect(‘Foo\\Bar\\MyClass’);\necho$reflectionClass->getShortName();//MyClass\necho$reflectionClass->getName();//Foo\\Bar\\MyClass\necho$reflectionClass->getNamespaceName();//Foo\\Bar\n

2.使用定义类的字符串加载

useRoave\\BetterReflection\\BetterReflection;\nuseRoave\\BetterReflection\\Reflector\\ClassReflector;\nuseRoave\\BetterReflection\\SourceLocator\\Type\\StringSourceLocator;\n$code='<?phpclassFoo{};’;\n$astLocator=(newBetterReflection())->astLocator();\n$reflector=newClassReflector(newStringSourceLocator($code,$astLocator));\n$reflectionClass=$reflector->reflect(‘Foo’);\necho$reflectionClass->getShortName();//Foo\n

3.通过指定PHP文件名来加载

useRoave\\BetterReflection\\BetterReflection;\nuseRoave\\BetterReflection\\Reflector\\ClassReflector;\nuseRoave\\BetterReflection\\SourceLocator\\Type\\SingleFileSourceLocator;\n$astLocator=(newBetterReflection())->astLocator();\n$reflector=newClassReflector(newSingleFileSourceLocator(‘path/to/MyApp/MyClass.php’,$astLocator));\n$reflectionClass=$reflector->reflect(‘MyApp\\MyClass’);\necho$reflectionClass->getShortName();//MyClass\necho$reflectionClass->getName();//MyApp\\MyClass\necho$reflectionClass->getNamespaceName();//MyApp\n

这种方式虽然传的是一个文件,但是在SingleFileSourceLocator::createLocatedSource()方法中会使用file_get_contents转换成字符串。

protectedfunctioncreateLocatedSource(Identifier$identifier):?LocatedSource\n{\nreturnnewLocatedSource(\nfile_get_contents($this->fileName),\n$this->fileName\n);\n}\n

其他的几个locator就不多介绍了。最后说一下如何反射一个闭包:

$myClosure=function(){\necho”Helloworld!\\n”;\n};\n$functionInfo=ReflectionFunction::createFromClosure($myClosure);\n$functionInfo->isClosure();//true\n

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!

Published by

风君子

独自遨游何稽首 揭天掀地慰生平