简体   繁体   English

在php中,有没有什么办法可以判断生成字符串时是否使用了::class?

[英]In php, is there any way to determine whether ::class was used when generating a string?

The two following return statements will return the same string:以下两个 return 语句将返回相同的字符串:

getClassName(): string
{
    return MyClassName::class; // returns 'MyClassName'
    return 'MyClassName';      // returns 'MyClassName'
}

We also have code that calls this method.我们也有调用这个方法的代码。

$className = getClassName();

In our code which calls this method, is there any way to determine whether ::class was used to generate the string?在我们调用这个方法的代码中,有没有办法判断::class是否被用来生成字符串?

IDE's that we use are able to detect when ::class is being used, and we would like to somehow use this logic in our code at compile time.我们使用的 IDE 能够检测::class何时被使用,我们希望在编译时以某种方式在我们的代码中使用这个逻辑。

IDEs can look at the source code and see what syntax you're using. IDE 可以查看源代码并查看您使用的语法。 They use this to provide hints, cross references, and all the other nice things they do while you're editing.他们使用它来提供提示、交叉引用以及他们在您编辑时所做的所有其他好事。

But once the code is compiled and running, most of this information is discarded.但是一旦代码被编译和运行,大部分信息都会被丢弃。 Values don't have any information about how they were calculated -- a string is just a bunch of characters.值没有任何关于它们是如何计算的信息——一个字符串只是一堆字符。 There are reflection features that allow you to access the call stack to see what functions were invoked, since this is needed in memory to implement returning, but data doesn't keep a trace of where the values came from.有一些反射功能允许您访问调用堆栈以查看调用了哪些函数,因为这需要在内存中实现返回,但数据不会跟踪值的来源。

So there's no built-in way to get the information you're looking for.所以没有内置的方法来获取您正在寻找的信息。 If you need to keep track of this, you'll have to implement it in the application code itself.如果您需要对此进行跟踪,则必须在应用程序代码本身中实现它。

getClassName(): array
{
    return ['source' => '::class', 'value' => MyClassName::class];
    return ['source' => 'string', 'value' => 'MyClassName'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以检查类是否在PHP中具有属性吗? - Is there any way to check whether the class has attribute in PHP? PHP:确定类是否为另一类的扩展 - PHP: Determine whether class is extension of another class 有什么简单的方法可以确定函数是对引用还是按值调用? - Any easy way to determine whether functions work on reference or call by value? 有什么方法可以确定对数据库行的更新是由 Web 应用程序(php 进程)执行的,还是通过 DB GUI 手动运行的 - Is there any way to determine whether an update to a database row was performed by a web application (php process), or run by hand through a DB GUI 在php中,是否有任何方法可以确定传入的HTTP请求是POST还是GET? - In php, is there any method to determine whether the incoming http request is POST or GET? 确定FQCN是类,接口还是特征的最佳方法 - Best way to determine whether a FQCN is a class, interface, or trait 确定文件是否是PHP中的图像的最佳方法是什么? - What is the best way to determine whether or not a file is an image in PHP? PHP如何确定变量是数组还是字符串? - How does PHP determine whether a variable is an array or a string? PHP检查字符串是否为空的最佳方法 - PHP best way to check whether a string is empty or not 有没有办法在PHP中检查是否有任何电子邮件是活动的? - Is there any way to check, in PHP, that whether any email is active or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM