PHP:等效于字符串表示的类名的 instanceof

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4618550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:42:49  来源:igfitidea点击:

PHP: instanceof equivalent for a string-represented classname

php

提问by shealtiel

It's best to explain by an example. I look for a function "theFunctionILookFor" that will make the code work.

最好通过一个例子来解释。我寻找一个函数“theFunctionILookFor”来使代码工作。

$myClassName = "someName";
$parentOrInterfaceName = "someParent";
if ( theFunctionILookFor ($myClassName))  echo "is parrent";

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

编辑:我尽量避免实例化类只是为了执行此检查。我希望能够将 2 个字符串参数传递给检查函数

回答by shealtiel

Looks like this is my answer: is_subclass_of

看起来这是我的答案: is_subclass_of

It can accept both parameters as strings

它可以接受两个参数作为字符串

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

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

回答by Unsigned

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

试试is_subclass_of()。它可以接受两个参数作为字符串。http://php.net/manual/en/function.is-subclass-of.php

回答by Kmtdk

I think that instanceofis a lot faster and more clear to read.

我认为instanceof阅读起来更快,更清晰。

instanceofdoes not work on strings, because it can be used as a "identifier" for a class:

instanceof不适用于字符串,因为它可以用作类的“标识符”:

 $temp = 'tester';
 $object  = new sub();
 var_dump($object instanceof $temp); //prints "true" on php 5.3.8

 class tester{}

 class sub extends \tester{

 }

Also, it works on interfaces - you just need a variable to contain the absolute class name (including namespace, if you want to be 100% sure).

此外,它适用于接口 - 您只需要一个变量来包含绝对类名(包括命名空间,如果您想 100% 确定)。

回答by Oswald

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

使用Reflection API,您可以使用类名和对象构造一个 ReflectionClass对象。

回答by Nanne

You might use this?

你可能会用这个?

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

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.

get_parent_class

(PHP 4, PHP 5, PHP 7)
get_parent_class — 检索对象或类的父类名

描述

string get_parent_class ([ mixed $object ] )

检索对象或类的父类名称。

回答by TCB13

This is an old thread, but still for future reference, you can use the functions is_aand is_subclass_ofto accomplish this in a more efficient way. No need to instantiate anything, you can just pass your string like:

这是一个旧线程,但仍供将来参考,您可以使用这些函数is_ais_subclass_of以更有效的方式完成此操作。无需实例化任何东西,您只需传递您的字符串,例如:

if (is_subclass_of($myObject, 'SomeClass')) // is_a(...)
  echo "yes, $myObject is a subclass of SomeClass\n";
else
  echo "no, $myObject is not a subclass of SomeClass\n";

PHP Docs:

PHP 文档:

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