I want to check if a class is a subclass of another without creating an instance. I have a class that receives as a parameter a class name, and as a part of the validation process, I want to check if it's of a specific class family (to prevent security issues and such). Any good way of doing this?
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Keeping track of variable instances
is_subclass_of()
will correctly check if a class extends another class, but will not returntrue
if the two parameters are the same (is_subclass_of('Foo', 'Foo')
will befalse
).A simple equality check will add the functionality you require.
You can use
is_a()
with the third parameter$allow_string
that has been added in PHP 5.3.9. It allows a string as first parameter which is treated as class name:Example:
Output:
Demo: http://3v4l.org/pGBkf
You need to use is_subclass_of() function to find that out. Please check below sample code.
-- The output for the same would be:
Please note that no output will be printed for last line
is_subclass_of($f,'Bar')
which will make it fail in conditional checking.If you want to see their boolean values, you need to use
var_dump()
instead ofprint()
function.Please check this link for more information
Check out
is_subclass_of()
. As of PHP5, it accepts both parameters as strings.You can also use
instanceof
, It will return true if the class or any of its descendants matches.Yup, with Reflection