简体   繁体   English

PHP代码中的无法解释的类型提示

[英]Inexplicable type hinting in PHP code

I'm analyzing some PHP code which is running on a server I don't have full access to. 我正在分析一些在我无法完全访问的服务器上运行的PHP代码。 I can read the phpinfo though. 我可以阅读phpinfo。 The code seems to run fine on the server. 代码似乎在服务器上正常运行。 In my local environment I just can't get the code to run as I get a "Catchable Fatal Error" at some call of a method using type hinting. 在我的本地环境中,我无法获取代码运行,因为我在使用类型提示的方法调用时得到“可捕获的致命错误”。

someMethod(string $str) {
  // Do something...
}

The error says the following: "Argument 1 passed to ... must be an instance of path\\of\\namespace\\string, string given ...". 该错误说明如下:“传递给...的参数1必须是路径\\的\\ namespace \\ string,给定字符串......”的实例。

There is no use keyword with a string class nor can I find anything trough a grep command in the folders of the development environment. 没有use字符串类的关键字,也无法通过开发环境的文件夹中的grep命令找到任何内容。

Are there any PHP modules, extensions that can make such a type hinting work? 是否有任何PHP模块,扩展可以使这种类型提示工作? The server and my development environment are using PHP 5.4.25. 服务器和我的开发环境使用的是PHP 5.4.25。

What could the live system possibly provide to make such code run? 实时系统可能提供什么来运行这样的代码? Might it use some other programming language based on PHP like Hack? 它可能会使用其他一些基于PHP的编程语言,比如Hack吗? The rest of the code is pretty straight PHP! 其余代码非常直接PHP!

You mention that there is no use statement or namespace declaration that alludes to a 'string' class in the code. 您提到没有使用声明或命名空间声明来暗示代码中的“字符串”类。 Does the code use an Autoloader? 代码是否使用自动加载器?

Two possible issues at hand here: 这里有两个可能的问题:

Paths - It is possible that the live environment has another path set up, and/or that a file/class is being loaded/searched for via this 'other path' (outside the code you may be looking at). 路径 - 实时环境可能设置了另一个路径,和/或正在通过此“其他路径”(在您可能正在查看的代码之外)加载/搜索文件/类。

Error Handling - Another possible cause is if there is an error handler in the production environment that always returns true. 错误处理 - 另一个可能的原因是生产环境中的错误处理程序始终返回true。

I had this exact issue where a type-hint wasn't resolving in development, but I didn't realize until we pushed it live and the error handler was no longer registered. 我有一个确切的问题 ,其中类型提示没有在开发中解决,但我没有意识到,直到我们推送它并且错误处理程序不再注册。

Obviously a fatal parse error can not be stopped, but it turns out a catch-able error can be ignored if the error handler returns true. 显然,无法停止致命的解析错误,但事实证明,如果错误处理程序返回true,则可以忽略可捕获的错误。 And this is what I am putting my money on in your case. 这就是我在你的案子中投入的资金。

Additionally, it is very important to note that there is no way to type-hint a scalar, as one user pointed out. 此外,正如一位用户所指出的那样,非常重要的是要注意无法键入标量标量。

A simpler way to say it is "it is not possible to type-hint anything that can be represented by a string." 更简单的说法是“不可能输入任何可以用字符串表示的东西。” This is due to the way in which PHP handles it's more primitive variables, they can all be type-cast to one another, and therefore (because 1 , "1" and true can all be == 1 , == '1' and == true ) it is not actually possible for the interpreter as it is written to actually catch and enforce scalar type-hints. 这是由于PHP处理它的原始变量的方式,它们都可以彼此进行类型转换,因此(因为1"1"true都可以是== 1== '1'== true )解释器实际上是不可能的,因为它被编写为实际捕获并强制执行标量类型提示。

Answer this question: Is that variable supposed to be $str = "something"; 回答这个问题:这个变量应该是$str = "something"; or $str = new string(); $str = new string(); (ie, a string or an object)? (即一个字符串或一个对象)?

If it is supposed to be a string, remove the type-hint, as nothing in PHP allows this support (save for HHVM, but you would know this if you were using it). 如果它应该是一个字符串,请删除type-hint,因为PHP中没有任何内容允许这种支持(除了HHVM,但如果你使用它,你会知道这一点)。

Since you say you do not have access to the code, I suggest notifying someone who does. 既然你说你无法访问代码,我建议你通知某人。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM