php输入框动态提示_在PHP中输入提示

php输入框动态提示

Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

从PHP 5开始,您可以使用类型提示来指定函数声明中参数的预期数据类型。 当您调用该函数时,PHP将检查参数是否为指定的类型。 否则,运行时将引发错误并且执行将被暂停。

Valid types are class names for arguments that receive objects and array for those that receive arrays. Here’s an example:

有效类型是用于接收对象的参数的类名和用于接收数组的参数的类名。 这是一个例子:

<?php
function enroll(Student $student, School $school) {
echo "Enrolling " . $student->name . " in " . $school->name;
}

By telling PHP exactly what kind of objects the enroll() method expects to receive, you can ensure that students are being enrolled in a school instead of a nunnery or a 401K. Likewise, you know that you won’t have any stubborn pachyderms showing up for the first day of third grade.

通过确切告诉PHP enroll()方法希望接收哪种对象,可以确保学生正在就读学校,而不是修女院或401K。 同样,您知道三年级的第一天不会出现任何顽固的上皮。

What would happen if I tried to enroll myself into medicare?

如果我尝试参加医疗保险会怎样?

<?php
$me = new Student("Amanda");
$medicare = new Program("Medicare");
$enroll = enroll($me, $medicare);

Although I am a student, the following error would occur:

虽然我是学生,但会发生以下错误:

Catchable fatal error: Argument 2 passed to enroll() must be an instance of School, instance of Program given, called in typehint.php on line 32 and defined in typehint.php on line 6

If null is used as a default value for an argument it will also be allowed. Here’s an example, this time with arrays:

如果 null用作参数的默认值,也将被允许。 这是一个例子,这次是数组:

<?php
function startParty(array $guests, array $food = null) {
    // party stuff...
}
$guests = array("Susan Foreman", "Sarah Jane Smith", "Rose Tyler", "Donna Noble");
startParty($guests, null);

There’ll be a party as long as there are guests, with or without food.

只要有客人吃饭或不吃饭都会举行聚会。

提示的局限性 (Limitations of Hinting)

Any defined class can be a valid type hint, though PHP does not support type hinting for a generic object. What about everything else?

任何定义的类都可以是有效的类型提示,尽管PHP不支持通用对象的类型提示。 那其他的呢?

Here is a peculiar example of the limitations of PHP’s type hinting:

这是PHP类型提示的局限性的一个特殊示例:

<?php
function stringTest(string $string) {
    echo $string;
}
stringTest("definitely a string");
Catchable fatal error: Argument 1 passed to stringTest() must be an instance of string, string given, called in typehint.php on line 42 and defined in typehint.php on line 39

You’re not the first think “What is this madness? I gave you a string instance, and yet you complain it must be an instance of string!” It’s alright. It happens to the best of us. In fact, it can be quite a confusing error message at first glance.

您不是第一个想到“这是什么疯狂? 我给了你一个字符串实例,但你抱怨它一定是字符串实例!” 没关系。 它发生在我们最好的人身上。 实际上,乍一看,这可能是一个非常令人困惑的错误消息。

stringTest() is not looking for a string, it is looking for an instance of a string class. Scalar data types, such as strings or integer values, are not supported in PHP’s type hinting. But it’s okay! If you need to raise an error or throw an exception when an argument is not a scalar type (like a string or integer), you can do perform basic validation to serve this purpose using functions like is_string() or is_int().

stringTest()不是在寻找字符串,而是在寻找string类的实例。 PHP的类型提示不支持标量数据类型,例如字符串或整数值。 不过没关系! 如果在参数不是标量类型(例如字符串或整数)时需要引发错误或引发异常,则可以使用is_string()is_int()类的函数执行基本验证来实现此目的。

标量战争 (The Scalar Wars)

There has been a bit of controversy regarding the addition of scalar PHP type-hinting in PHP 5.4. Those who oppose the change argue that this support would go against the fundamental designs of PHP. PHP is considered to be a weak typed language. In essence, this means that PHP does not require you to declare data types. Variables still have data types associated with them but you can do radical things like adding a string to an integer without resulting in an error.

关于在PHP 5.4中添加标量PHP类型提示存在一些争议。 反对更改的人认为,这种支持将与PHP的基本设计背道而驰。 PHP被认为是一种弱类型语言。 从本质上讲,这意味着PHP不需要您声明数据类型。 变量仍然具有与之关联的数据类型,但是您可以做一些根本性的事情,例如将字符串添加到整数而不会导致错误。

In May of 2010 support for scalar type hinting was added to the PHP trunk. But because of community response this feature will not make its way into the 5.4 release.

在2010年5月,对标量类型提示的支持已添加到PHP主干。 但是由于社区的反馈,此功能将不会进入5.4版本。

摘要 (Summary)

Type hinting is a technique introduced into PHP for object-oriented programming (specifically for identifying the type of a caught exception). I encourage you to read more about working with objects here.

类型提示是PHP引入的一种技术,用于面向对象的编程(特别是用于标识捕获的异常的类型)。 我鼓励您在这里阅读更多有关使用对象的信息。

Image via Carlos E. Santa Maria / Shutterstock

图片来自Carlos E.Santa Maria / Shutterstock

翻译自: https://www.sitepoint.com/type-hinting-in-php/

php输入框动态提示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值