为什么在PHP中需要类型提示

为什么在PHP中需要类型提示

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

问题描述

我很难理解PHP中类型提示的重要性.

I am having trouble wrapping my head around the importance of Type hinting in PHP.

显然,PHP中的类型提示"可以定义如下:

Apparently 'type hinting' in PHP can be defined as follows:

因此,在最基本的级别上键入提示,实际上并不需要使代码正常工作吗?

我有以下代码来尝试了解正在发生的事情...

I have the following code to try and understand what is going on...

Index.php

<?php
include 'Song.php';

$song_object = new Song;

$song_object->title = "Beat it!";
$song_object->lyrics = "It doesn't matter who's wrong or right... just beat it!";


function sing(Song $song)
{
    echo "Singing the song called " . $song->title;
    echo "<p>" . $song->lyrics . "</p>";
}

sing($song_object);

Song.php

<?php

class Song
{
    public $title;
    public $lyrics;
}

无论函数sing()是否带有小小的类型提示,代码都会执行其操作

the code does its thing with or without the little type hint in the function sing();

因此,这使我相信类型提示只是一种编码约定,以确保仅使用某些类,而无需使用某些类来生成功能代码,这是正确的吗?

So, this leads me to believe that type hinting is simply a coding convention to make sure only certain classes are used and are not needed to produce functional code, is this correct?

类型提示可以在您与团队合作的情况下创建标准.

Type hinting, as the quote above suggests, is there to create a standard if you're working with a team.

我在这里想念东西吗?

推荐答案

类型提示不是必需的,但它可以使您捕获某些类型的错误.例如,您可能有一个需要整数的函数或方法. PHP将愉快地将寻找数字的字符串"转换为整数,这会导致难以调试的行为.如果您在代码中指定特别需要一个整数,则可以从一开始就防止出现此类错误.许多程序员认为以这种方式保护自己的代码是一种最佳做法.

Type hinting isn't required, but it can allow you to catch certain types of mistakes. For example, you might have a function or method which requires an integer. PHP will happily convert "number looking strings" into integers, and this can cause hard to debug behaviour. If you specify in your code that you specifically need an integer, this can prevent those kinds of bugs in the first place. Many programmers consider protecting their code in this way to be a best practice.

作为此操作的具体示例,让我们看一下index.php文件的更新版本:

As a concrete example of this in action, let's look at an updated version of your index.php file:

index.php

<?php
include 'Song.php';
include 'Test.php';

$song_object = new Song;
$test_object = new Test;

$song_object->title = "Beat it!";
$song_object->lyrics = "It doesn't matter who's wrong or right... just beat it!";

$test_object->title = "Test it!";
$test_object->lyrics = "It doesn't matter who's wrong or right... just test it!";


function sing(Song $song)
{
    echo "Singing the song called " . $song->title;
    echo "<p>" . $song->lyrics . "</p>";
}

sing($song_object);
sing($test_object);

以及我添加的新Test.php文件:

Test.php

<?php

class Test
{
    public $title;
    public $lyrics;
}

现在运行index.php时,出现以下错误:

When I run index.php now, I get the following error:

输出:

Singing the song called Beat it!<p>It doesn't matter who's wrong or right...
just beat it!</p>PHP Catchable fatal error:  Argument 1 passed to sing() must
be an instance of Song, instance of Test given, called in test/index.php on
line 22 and defined in test/index.php on line 15

Catchable fatal error: Argument 1 passed to sing() must be an instance of
Song, instance of Test given, called in test/index.php on line 22 and defined
in test/index.php on line 15

这是PHP,让我知道调用sing()函数时尝试使用错误的类类型.

This is PHP letting me know that I tried to use the wrong type of class when I called the sing() function.

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

08-22 18:16