PHPis_subclass_of function BUG and solution _ PHP Tutorial

Source: Internet
Author: User
PHPis_subclass_of function BUG and solution. The function of is_subclass_of is as follows: boolis_subclass_of (objectobject, stringclass_name) if the object class is a subclass of class class_name, return Is_subclass_of:

The code is as follows:

Bool is_subclass_of (object, string class_name)


If the object belongs to a subclass of class class_name, TRUE is returned; otherwise, FALSE is returned.
Note: You can use a string to specify the object parameter (class name) from PHP 5.0.3 ).

Example:

The code is as follows:


# Determine whether $ className is a subclass of $ type
Is_subclass_of ($ className, $ type );

Php5.3.7 may have a bug in interface

Bug: https://bugs.php.net/bug.php? Id = 53727

The code is as follows:


Interface MyInterface {}
Class ParentClass implements MyInterface {}
Class ChildClass extends ParentClass {}

# True
Is_subclass_of ('childclass', 'myinterface ');
# False
Is_subclass_of ('parentclass', 'myinterface ');

Solution:

The code is as follows:

Function isSubclassOf ($ className, $ type ){
// If $ className belongs to a subclass of $ type, TRUE is returned.
If (is_subclass_of ($ className, $ type )){
Return true;
}

// If php version> = 5.3.7 does not have an interface bug, $ className is not a subclass of $ type.
If (version_compare (PHP_VERSION, '5. 3.7 ','> = ')){
Return false;
}

// If $ type is not an interface, there will be no bug. Therefore, $ className is not a subclass of $ type.
If (! Interface_exists ($ type )){
Return false;
}

// Create a reflection object
$ R = new ReflectionClass ($ className );
// Determine whether the class belongs to the $ type interface through the reflection object
Return $ r-> implementsInterface ($ type );
}

The authorization code is as follows: bool is_subclass_of (object, string class_name). If the object belongs to a subclass of class class_name, return...

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.