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