是否可以编写严格类型的PHP代码? [英] Is it possible to write strictly typed PHP code?

查看:51
本文介绍了是否可以编写严格类型的PHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,是否可以编写如下代码:

For example, is it possible to write code like this:

int $x = 6;
str $y = "hello world";
bool $z = false;
MyObject $foo = new MyObject();

是否可以定义如下函数:

And is it possible to define functions like this:

public int function getBalance()
{
   return 555; //Or any numeric value
}

推荐答案

编辑:该答案适用于PHP 5.6和更低版本.如最近的回答所述,PHP 7.0及更高版本的确对此提供了一些支持

This answer applies to versions of PHP 5.6 and earlier. As noted in recent answers, PHP version 7.0 and later does have some support for this

原始答案:

不.自php5起,仅支持类型提示,但类型提示只能是对象和数组类型(自PHP 5.1起).不支持使用int和string的传统类型提示."

No. There is only support for type hinting since php5, but "Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported."

在PHP 7中实现了标量类型声明",请参见下面的答案.

In PHP 7 are implemented "Scalar Type Declarations" see the answer below.

这是目前php所能达到的范围,如果您问我的话,它所能达到的范围也可以.

That is as far as php currently goes, and as far as it should go if you ask me.

这篇关于是否可以编写严格类型的PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆