Important knowledge points in PHP object-oriented (iii) _php tutorial

Source: Internet
Author: User
1. Namespace: Similar to the namespaces in C + +, the same is true in order to avoid name collisions that can be caused by referencing more third-party libraries. Through namespaces, even though the names of the two classes are the same, they can still be precisely positioned and differentiated because they are in different namespaces. The first time you see the namespace syntax of PHP, the feeling and C + + in the syntax is very very similar, but in writing a little example to do the experiment when it was found that their differences are very large, in order to avoid forgetting later, so this special recorded it down. See the following code: copy code GetName (); if ($c->isuserdefined ()) {$details. = "$name is User defined.\n",} if ($c->isinternal ()) {$details. = "$name is b Uilt-in.\n "; if ($c->isabstract ()) {$details. = "$name is abstract class.\n",} if ($c->isfinal ()) {$details. = "$name is fin Al class.\n "; if ($c->isinstantiable ()) {$details. = "$name can be instantiated.\n";} else {$details. = "$name cannot is instant Iated.\n "; } return $details;} function Classsource (Reflectionclass $c) {$path = $c->getfilename (); $lines = @file ($path);//Gets the starting and ending lines of the class definition code. $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";p rint ClassInfo (New Reflectionclass (' TestClass ')); Print "\nthe following is Class source.\n";p rint classsource (New Reflectionclass (' TestClass ')); The copy code runs as follows: Copy code Bogon: testphp$ PHP reflection_test.php The following is Class Information.testclass is user defined. Testclcan be instantiated. The following is Class source.class testclass {public $publicVariable; function Publicmethod () {print ' This is Publicmet Hod.\n "; The copy code below lets us continue the Reflectionmethod learning journey with code examples and critical annotations. Copy Code GetName (); $details = ""; if ($m->isuserdefined ()) {$details. = "$name is User defined.\n",} if ($m->isinternal ()) {$details. = "$name is b Uilt-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 Priva Te.\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. = "$nam E returns a reference.\n "; } return $details;} function Methodsource (Reflectionmethod $m) {$path = $m->getfilename (); $lines = @file ($path); $from = $m->getstartl INE (); $to = $m->getendline (); $len = $to-$from + 1; Return implode (Array_slice ($lines, $from-1, $len));} $RC = new Reflectionclass (' TestClass '); $methods = $RCGetMethods ();p rint "The following is method information.\n"; foreach ($methods as $method) {print methodInfo ($method); PRI NT "\ n--------------------\ n";} Print "The following is Method[testclass::p Ublicmethod] source.\n";p rint methodsource ($RC->getmethod (' Publicmethod '); The copy code runs as follows: Copy code bogon:testphp$ PHP reflection_test.php The following is method information.__construct Is User defined.__construct are Public.__construct is constructor. --------------------Privatemethod is User Defined.privatemethod is private. --------------------Publicmethod is User Defined.publicmethod was public. --------------------publicMethod2 is User defined.publicmethod2 was public. --------------------the following is Method[testclass::p Ublicmethod] source. function Publicmethod () {print "This is publicmethod.\n";} Copy the code let's continue Reflectionparameter, he represents the parameter information for the member function. Go ahead and look at the code. Copy Code 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-&gt ; 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 ";} The copy code runs as follows: Copy code bogon:testphp$ PHP reflection_test.php $arg 1 have position 0. $arg 1 must be a Paramclass object $arg 2 have p Osition 1. $arg 2 is passed by reference. $arg 3 has position 2. $arg 3 have default: The copy code described above is a PHP-provided reflection API to traverse any class of specific information, in fact, and other languages such as Java provides the same reflection capabilities, PHP also supports the method of invoking the actual object through the reflection class, which is mainly applied to two methods, namely Reflectionclass::nEwinstance () Creates an object instance, and the other is Reflectionmethod::invoke (), which executes the method based on the object instance and method name. See the following code: copy code Privatearg = $arg; } function Publicmethod () {print ' $privateArg = '. $this->privatearg. " \ n "; } function PublicMethod2 ($arg 1, $arg 2) {print ' $arg 1 = '. $arg 1. ' $arg 2 = '. $arg 2. " \ n "; }} $RC = new Reflectionclass (' TestClass '); $testObj = $RC->newinstanceargs (Array (' This is private argument. ')); $method = $RC->getmethod (' Publicmethod '); $method->invoke ($TESTOBJ); $method 2 = $RC->getmethod (' publicMethod2 '); $method 2->invoke ($TESTOBJ, "Hello", "World"); the copy code runs as follows: Bogon: testphp$ php reflection_test.php $PRIVATEARG = This is private argument. $arg 1 = Hello $arg 2 = World In fact Reflectionclass, REF Lectionmethod and Reflectionparameter provide us with more available methods, here are just a few of the most typical methods, so that we can more intuitively learn and understand the PHP Reflection API. I believe after reading the code after the example, we will be more clear, if you need to use the class-related features in the future, from the Reflectionclass, and member function information must come from Reflectionmethod, The method parameter information is derived from the Reflectionparameter. Note: The knowledge recorded in this blog is in the process of learning PHP, I encountered some of the PHP and other object-oriented language compared to the more unique places, or for me I do need to write down the book to prepare for the post-search knowledge points. Although there is no depth, but still hope to share with you.

http://www.bkjia.com/PHPjc/664271.html www.bkjia.com true http://www.bkjia.com/PHPjc/664271.html techarticle 1. Namespace: Similar to the namespaces in C + +, the same is true in order to avoid name collisions that can be caused by referencing more third-party libraries. Through namespaces, even two cl ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.