A small problem with the PHP reflection API

Source: Internet
Author: User
The manual that writes the Reflectionclass Issubclass method in the PHP reflection API is a string type, but why is it possible to pass the Reflectionclass object in?

class test1 {    public $a;}class test1 extends test{    public $b;}$object1 = new ReflectionClass('test1');$object2 = new ReflectionClass('test2');var_dump($object2 -> isSubclassOf($object2));var_dump($object2 -> isSubclassOf('test1'));

Both of these outputs are Boolean true, and the required arguments are strings, but it is also possible to pass the object parameters, which is why?

Reply content:

The manual that writes the Reflectionclass Issubclass method in the PHP reflection API is a string type, but why is it possible to pass the Reflectionclass object in?

class test1 {    public $a;}class test1 extends test{    public $b;}$object1 = new ReflectionClass('test1');$object2 = new ReflectionClass('test2');var_dump($object2 -> isSubclassOf($object2));var_dump($object2 -> isSubclassOf('test1'));

Both of these outputs are Boolean true, and the required arguments are strings, but it is also possible to pass the object parameters, which is why?

Because in the source code of PHP here is the two types can be passed in, the first is the string second is Reflectionclass.
Address on GitHub: GitHub

It is clear in the code that you can see in the comments

/* {{{ proto public bool ReflectionClass::isSubclassOf(string|ReflectionClass class)   Returns whether this class is a subclass of another class */

and is judged in the next switch statement.

switch (Z_TYPE_P(class_name)) {        case IS_STRING:            //some code        case IS_OBJECT:            //some code        default:            //some code    }

PHP has a lot of external maintainers, so the document is not very detailed is also normal, a lot of things need to try their own.

Probably the manual didn't write it all.
IS_SUBCLASS_OF estimates refer to this function, which says that the object can be passed.

This is some of PHP's vulnerabilities (also considered an advantage, after all, the spirit of flexibility).
It is recommended to follow the manual upload parameters, because these small loopholes will be repaired in subsequent releases.
Or you can go to the English document of the PHP manual, which is more authoritative than the Chinese document

  • 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.