为什么没有内置类型的类型提示? [英] Why no type hints for built-in types?

查看:88
本文介绍了为什么没有内置类型的类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我是新来的,对不起,如果之前已经讨论过;我没有找到它/ b $ b搜索PHP组。 (我也读过建议交叉发布到其他PHP组的

,但如果不鼓励,请告诉我。在

的同时,那么请让我知道要发布的许多PHP组中的哪一个。

:))


在PHP5中,您可以提供类型提示。对于函数,像这样:


class Person {...}


函数f(Person $ p)

$

...

}


因为这是对象的可选静态类型,为什么不做同样的

功能适用于所有类型,内置类型包括?


我来自一个静态和强类型的背景(C ++,

Java),并且已经使用了PHP几年,我已经有好几次因为静态类型可能被捕获的愚蠢错误而感到被咬伤了,例如

传递一个空字符串 - 在算术

上下文中转换为0,当该函数应该接收一个数字时,或者某些数字,

并且没有报告错误。这些错误很难找到。


这在一些Q&在Zend的'',例如:
http://www.zend.com/expert_qa/qas.php?id=104&single=1


---开始引用---


将支持简单类型的类型提示,例如

class Foo {

公共功能栏( int $ var){}

}


否,不支持输入简单类型的提示。原因是

PHP的动态特性。发布到脚本的数字将以字符串

到达,即使它是一个数字。在这种情况下,PHP假定10为10。和10个是b $ b同样的事情。有这样的类型提示不适合这个

自动转换PHP。


---结束语---

我不觉得这个答案令人满意。是的,PHP有松散/弱的输入,但是任何时候都有
,值或变量具有不同的类型。在上面引用的示例

中,您必须确保您传递的值是

正确的类型。


这也会打开超载的大门,虽然看起来好像来自Andi和Zeev在Zend论坛上的回复,既没有可选的静态输入,也没有输入b $ b此时考虑超载,也可能不会在未来的b $ b b中。 :/我所看到的反对它的论点,我还没有发现

足够令人信服,因此我想听听专业人士和

cons可选的静态类型,可能是重载(但是,

应该是一个单独的线程)。 PHP手册所谓的重载

与其他OO

语言(例如C ++ / Java)中的重载概念实际上没有任何关系。


关于在
Python中添加可选静态类型的讨论非常热烈( http://www.artima.com/weblogs/viewpo...?thread=85551) ,除非
它已经是
了,也许现在是我们考虑PHP的时候了。

PHP5中当前的静态类型检查是相当半生不熟的,

仅涵盖用户定义的类型。


问候,


Terje

解决方案

p)

{

...

}


因为这是对象的可选静态类型,为什么不为所有类型提供相同的

功能,包括内置类型?


我来自一个静态和强类型的背景(C ++,
Java),并且使用PHP几年,我已经很多次了

被可能被静态输入捕获的愚蠢错误所咬,例如传递空字符串
- 在算术中转换为0

上下文,当函数应该接收一个数字,或者一些这样的时候,

并且没有报告错误。这些错误很难找到。


这在一些Q&在Zend的'',例如:
http://www.zend.com/expert_qa/qas.php?id=104&single=1


---开始引用---


将支持简单类型的类型提示,例如

class Foo {

公共功能栏( int


var){}

}


否,不支持输入简单类型的提示。原因是

PHP的动态特性。发布到脚本的数字将以字符串

到达,即使它是一个数字。在这种情况下,PHP假定10为10。和10个是b $ b同样的事情。有这样的类型提示不适合这个

自动转换PHP。


---结束语---

我不觉得这个答案令人满意。是的,PHP有松散/弱的输入,但是任何时候都有
,值或变量具有不同的类型。在上面引用的示例

中,您必须确保您传递的值是

正确的类型。


这也会打开超载的大门,虽然看起来好像来自Andi和Zeev在Zend论坛上的回复,既没有可选的静态输入,也没有输入b $ b此时考虑超载,也可能不会在未来的b $ b b中。 :/我所看到的反对它的论点,我还没有发现

足够令人信服,因此我想听听专业人士和

cons可选的静态类型,可能是重载(但是,

应该是一个单独的线程)。 PHP手册所谓的重载

与其他OO

语言(例如C ++ / Java)中的重载概念实际上没有任何关系。


关于在
Python中添加可选静态类型的讨论非常热烈( http://www.artima.com/weblogs/viewpo...?thread=85551) ,除非
它已经是
了,也许现在是我们考虑PHP的时候了。

PHP5中当前的静态类型检查是相当半生不熟的,

仅涵盖用户定义的类型。


问候,


Terje


Terje Sletteb?写道:

我不觉得这个答案令人满意。是的,PHP有松散/弱的打字,但在任何时候,值或变量都有不同的类型。在上面引用的例子中,你必须确保你传递的值是正确的类型。




没有宽松的打字。以下示例将打印6,因为PHP将会尝试将输入转换为适当的类型:


print 2 *" 3将打印6"


如果要测试类型,请使用is_ *函数。是的,这并不像在Java中那样工作,但PHP不是Java。

JW


Hi.

I''m new here, and sorry if this has been discussed before; I didn''t find it
searching the PHP groups. (I''ve also read recommendations to cross-post to
the other PHP groups, but if that is discouraged, please let me know. At the
same time, please then let me know which of the many PHP groups to post to.
:) )

In PHP5, you can provide "type hints" for functions, like this:

class Person {...}

function f(Person $p)
{
...
}

Since this is optional static typing for objects, why not make the same
capability available for all types, built-in types included?

I come from a background with generally static and strong typing (C++,
Java), and having worked with PHP a couple of years, I''ve quite a few times
got bitten by stupid bugs that could have been caught by static typing, such
as passing an empty string - which gets converted to 0 in an arithmetic
context, when the function was supposed to receive a number, or some such,
and no error is reported. These bugs can be hard to find.

This has been suggested in a few Q & A''s at Zend, such as this one:
http://www.zend.com/expert_qa/qas.php?id=104&single=1

--- Start quote ---

Will be a support for type hints of simple types, like
class Foo{
public function bar(int $var) { }
}

No, type hints of simple types will not be supported. The reason is
PHP''s dynamic nature. A number posted to a script will arrive as a string
even though it''s a number. In this case, PHP assumes that "10" and 10 are
the same thing. Having such type hints would not fit into this
auto-conversion of PHP.

--- End quote ---

I don''t find this answer satisfactory. Yes, PHP has loose/weak typing, but
at any one time, a value or a variable has a distinct type. In the example
in the quote above, you''d have to ensure that the value you pass is of the
right type.

This would also open the door to overloading, although it seems from the
replies from Andi and Zeev in the Zend forums that neither optional static
typing, nor overloading is considered at this time, and likely not in the
future, either. :/ What I have seen of arguments against it, I haven''t found
sufficiently convincing, so therefore I''d like to hear about the pros and
cons of optional static typing, and possibly overloading (however, that
should really be a separate thread). What the PHP manual calls "overloading"
has really nothing to do with the concept of overloading in other OO
languages, such as C++/Java.

There''s a rather lively discussion about adding optional static typing in
Python (http://www.artima.com/weblogs/viewpo...?thread=85551), and unless
it has already been, maybe it''s time for us to consider it for PHP, as well.
The current static type checking in PHP5 is something rather half-baked,
only covering user-defined types.

Regards,

Terje

解决方案

p)
{
...
}

Since this is optional static typing for objects, why not make the same
capability available for all types, built-in types included?

I come from a background with generally static and strong typing (C++,
Java), and having worked with PHP a couple of years, I''ve quite a few times
got bitten by stupid bugs that could have been caught by static typing, such
as passing an empty string - which gets converted to 0 in an arithmetic
context, when the function was supposed to receive a number, or some such,
and no error is reported. These bugs can be hard to find.

This has been suggested in a few Q & A''s at Zend, such as this one:
http://www.zend.com/expert_qa/qas.php?id=104&single=1

--- Start quote ---

Will be a support for type hints of simple types, like
class Foo{
public function bar(int


var) { }
}

No, type hints of simple types will not be supported. The reason is
PHP''s dynamic nature. A number posted to a script will arrive as a string
even though it''s a number. In this case, PHP assumes that "10" and 10 are
the same thing. Having such type hints would not fit into this
auto-conversion of PHP.

--- End quote ---

I don''t find this answer satisfactory. Yes, PHP has loose/weak typing, but
at any one time, a value or a variable has a distinct type. In the example
in the quote above, you''d have to ensure that the value you pass is of the
right type.

This would also open the door to overloading, although it seems from the
replies from Andi and Zeev in the Zend forums that neither optional static
typing, nor overloading is considered at this time, and likely not in the
future, either. :/ What I have seen of arguments against it, I haven''t found
sufficiently convincing, so therefore I''d like to hear about the pros and
cons of optional static typing, and possibly overloading (however, that
should really be a separate thread). What the PHP manual calls "overloading"
has really nothing to do with the concept of overloading in other OO
languages, such as C++/Java.

There''s a rather lively discussion about adding optional static typing in
Python (http://www.artima.com/weblogs/viewpo...?thread=85551), and unless
it has already been, maybe it''s time for us to consider it for PHP, as well.
The current static type checking in PHP5 is something rather half-baked,
only covering user-defined types.

Regards,

Terje


Terje Sletteb? wrote:

I don''t find this answer satisfactory. Yes, PHP has loose/weak
typing, but at any one time, a value or a variable has a distinct
type. In the example in the quote above, you''d have to ensure that
the value you pass is of the right type.



Not with loose typing. The following example will print 6 because PHP will
try to convert the input to the approriate types:

print 2 * "3 will print 6";

If you want to test for types, use the is_* functions. Yes, this doesn''t
work the same as in Java, but PHP isn''t Java.
JW


这篇关于为什么没有内置类型的类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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