Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instanceof equivalent for a string-represented classname

Tags:

php

I'm looking for a functionality, in the example below called "theFunctionILookFor", that will make the code work.

$myClassName = "someName";
$parentOrInterfaceName = "someParent";
if (theFunctionILookFor($myClassName)) {
    echo "is parent";
}

Edit: I try to avoid instantiating the class just to perform this check. I would like to be able to pass 2 string parameters to the checking function

like image 921
shealtiel Avatar asked Jan 06 '11 18:01

shealtiel


4 Answers

Looks like this is my answer: is_subclass_of

It can accept both parameters as strings

http://php.net/manual/en/function.is-subclass-of.php

like image 55
shealtiel Avatar answered Nov 02 '22 03:11

shealtiel


Try is_subclass_of(). It can accept both parameters as strings. http://php.net/manual/en/function.is-subclass-of.php

like image 20
Unsigned Avatar answered Nov 02 '22 02:11

Unsigned


Using the Reflection API, you can construct a ReflectionClass object using the name of the class as well as an object.

like image 4
Oswald Avatar answered Nov 02 '22 02:11

Oswald


You might use this?

http://www.php.net/manual/en/function.get-parent-class.php

From the page:

get_parent_class

(PHP 4, PHP 5, PHP 7)
get_parent_class — Retrieves the parent class name for object or class

Description

string get_parent_class ([ mixed $object ] )

Retrieves the parent class name for object or class.

like image 3
Nanne Avatar answered Nov 02 '22 01:11

Nanne