gpt4 book ai didi

php - PHP 中的严格类型有什么作用?

转载 作者:IT老高 更新时间:2023-10-28 12:01:28 32 4
gpt4 key购买 nike

我在 PHP 7 中看到了以下新行,但没有人真正解释它的含义。我用谷歌搜索了它,他们谈论的只是你会启用它还是不喜欢投票类型的东西。

declare(strict_types = 1);

它有什么作用?它如何影响我的代码?我应该这样做吗?

解释一下就好了。

最佳答案

来自 Treehouse blog :

With PHP 7 we now have added Scalar types. Specifically: int, float, string, and bool.

By adding scalar type hints and enabling strict requirements, it is hoped that more correct and self-documenting PHP programs can be written. It also gives you more control over your code and can make the code easier to read.

By default, scalar type-declarations are non-strict, which means they will attempt to change the original type to match the type specified by the type-declaration. In other words, if you pass a string that starts with a number into a function that requires a float, it will grab the number from the beginning and remove everything else. Passing a float into a function that requires an int will become int(1).

默认情况下,如果可能,PHP 会将错误类型的值转换为预期的标量类型。例如,为期望字符串的参数提供整数的函数将获得字符串类型的变量。

已禁用严格类型 (eval):

<?php

function AddIntAndFloat(int $a, float $b) : int
{
return $a + $b;
}

echo AddIntAndFloat(1.4, '2');
/*
* without strict typing, PHP will change float(1.4) to int(1)
* and string('2') to float(2.0) and returns int(3)
*/

可以在每个文件的基础上启用严格模式。在严格模式下,只会接受类型声明的确切类型的变量,否则会抛出 TypeError。此规则的唯一异常(exception)是可以将整数提供给期望 float 的函数。内部函数中的函数调用不受 strict_types 声明的影响。

要启用严格模式,请使用 declare 语句和 strict_types 声明:

已启用严格类型 (eval):

<?php declare(strict_types=1);

function AddIntAndFloat(int $a, float $b): int
{
return (string) $a + $b;
}

echo AddIntAndFloat(1.4,'2');
// Fatal error: Uncaught TypeError: Argument 1 passed to AddIntAndFloat() must be of the type int, float given
echo AddIntAndFloat(1,'2');
// Fatal error: Uncaught TypeError: Argument 2 passed to AddIntAndFloat() must be of the type float, string given

// Integers can be passed as float-points :
echo AddIntAndFloat(1,1);
// Fatal error: Uncaught TypeError: Return value of AddIntAndFloat() must be of the type integer, string returned

工作示例:

<?php

declare(strict_types=1);

function AddFloats(float $a, float $b) : float
{
return $a+$b;
}

$float = AddFloats(1.5,2.0); // Returns 3.5

function AddFloatsReturnInt(float $a, float $b) : int
{
return (int) $a+$b;
}

$int = AddFloatsReturnInt($float,1.5); // Returns 5

function Say(string $message): void // As in PHP 7.2
{
echo $message;
}

Say('Hello, World!'); // Prints "Hello, World!"

function ArrayToStdClass(array $array): stdClass
{
return (object) $array;
}

$object = ArrayToStdClass(['name' => 'azjezz','age' => 100]); // returns an stdClass

function StdClassToArray(stdClass $object): array
{
return (array) $object;
}

$array = StdClassToArray($object); // Returns array

function ArrayToObject(array $array): object // As of PHP 7.2
{
return new ArrayObject($array);
}

function ObjectToArray(ArrayObject $object): array
{
return $object->getArrayCopy();
}

var_dump( ObjectToArray( ArrayToObject( [1 => 'a' ] ) ) ); // array(1 => 'a');

关于php - PHP 中的严格类型有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48723637/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com