[英]PHP - Force function parameter to integer
In PHP you can do the following thing: 在PHP中,您可以执行以下操作:
class Something {// bla bla}
function functionName(Something $object) {
// Do stuff here ^^^^^^^^^
}
This ensures that function received an instance of Something class. 这样可以确保函数收到Something类的实例。
My problem is that I want to enforce to a basic type. 我的问题是我想强制使用基本类型。
function functionName(integer $someInt) {
// Do stuff here ^^^^^^^
}
functionName(5);
This says that $someInt is "not an instance of integer, integer given" (with my PHP version below 7.0). 这表示$ someInt是“不是整数的实例,给出了整数”(我的PHP版本低于7.0)。 How can you enforce to a basic type?
如何将其强制为基本类型?
You can't do this for scalar types see the manual: 对于标量类型,您不能执行此操作,请参见手册:
And a quote from there: 从那里引用:
Type hints cannot be used with scalar types such as int or string .
类型提示不能与int或string等标量类型一起使用 。 Resources and Traits are not allowed either.
资源和特质也不被允许。
But you will be able to to this with PHP 7: https://wiki.php.net/rfc/scalar_type_hints_v5 但是您可以使用PHP 7做到这一点: https : //wiki.php.net/rfc/scalar_type_hints_v5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.