我可以使用类型提示允许两种不同的类型吗?例如,参数1可以是2个类
function log (User|File $requester) {
}
Run Code Online (Sandbox Code Playgroud) 在测试我的php脚本是否与php-8兼容时,我遇到了以下代码:
function getDir($a, $o = 2) {
$d = Floor($a / $o);
return ($d % 2 === 0);
}
Run Code Online (Sandbox Code Playgroud)
在php-8之前,这工作正常,但是,在php-8 上它抛出:
致命错误:无法重新声明 getDir()
经过一段时间的搜索,我发现php-8引入了一个新的别名dir()
:
/** @param resource $context */
function getdir(string $directory, $context = null): Directory|false {}
Run Code Online (Sandbox Code Playgroud)
dir()
没有提到别名将我的 mac 更新为 php 8 laravel 应用程序后停止工作,这是我收到的错误:
Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 871
Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945
Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 871
Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945
Run Code Online (Sandbox Code Playgroud)
我试图通过调查代码来解决这个问题,但没有运气
FormRequest
我在 Laravel 项目中有一个非常简单的类。两种方法,两者都返回一个由方法部分填充的简单数组,但 IDE 对它们的处理方式不同。
<?php
namespace App\Http\Requests;
class VerifyPhoneNumber extends FormRequest {
use Support\ValidatesPhoneNumbers;
public function authorize(): bool {
return true;
}
public function rules(): array {
return [
"phone_number" => $this->getPhoneNumberRules(),
"verify_code" => ["required", "numeric", "max:999999"],
];
}
public function messages(): array {
return [
"phone_number.regex" => $this->getPhoneNumberMessage(),
];
}
}
Run Code Online (Sandbox Code Playgroud)
以及非常基本的特征方法:
<?php
namespace App\Http\Requests\Support;
trait ValidatesPhoneNumbers {
protected function getPhoneNumberMessage(): string {
return __("Some localized error message");
}
protected function getPhoneNumberRules(): array {
return ["regex:/^\+?1?[2-9][0-9]{5,14}$/", "max:16"]; …
Run Code Online (Sandbox Code Playgroud) 我正在将我的项目从 PHP 7.0 更新到 PHP 8.0,但我无法确定是否允许显式分配resource
为数据类型:
我现在所知道的是:
直到现在我读到:
我在某个地方错过了什么吗?
感谢您的时间。
为了更清楚起见,这就是我想要resource
在项目中使用数据类型的方式(PSR-7 实现):
<?php
namespace MyPackages\Http\Message;
use Psr\Http\Message\StreamInterface;
/**
* Stream.
*/
class Stream implements StreamInterface {
/**
* A stream, e.g. …
Run Code Online (Sandbox Code Playgroud) PHP 8 匹配表达式代码
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
Run Code Online (Sandbox Code Playgroud)
PHP 7 切换代码
switch (8.0) {
case '8.0':
$result = "Oh no!";
break;
case 8.0:
$result = "This is what I expected";
break;
}
echo $result;
//> Oh no!
Run Code Online (Sandbox Code Playgroud)
Deprecated: Required parameter $xxx follows optional parameter $yyy in...
Run Code Online (Sandbox Code Playgroud)
自从升级到 PHP 8.0 后,在运行这样的代码时会抛出这个错误:
function test_function(int $var1 = 2, int $var2) {
return $var1 / $var2;
}
Run Code Online (Sandbox Code Playgroud)
这在过去的 PHP 版本中没有问题。
pecl install apcu_bc
在 PHP 8 上安装包时遇到以下错误
In file included from /tmp/pear/temp/apcu_bc/php_apc.c:35:
/usr/local/include/php/ext/apcu/apc_arginfo.h:4:3: error: #error Not supported on PHP >= 8.0
# error Not supported on PHP >= 8.0
^~~~~
In file included from /usr/local/include/php/main/php.h:35,
from /tmp/pear/temp/apcu_bc/php_apc.c:23:
/usr/local/include/php/Zend/zend_API.h:85:69: error: 'zif_apcu_store' undeclared here (not in a function); did you mean 'zif_apc_dec'?
#define ZEND_FALIAS(name, alias, arg_info) ZEND_RAW_FENTRY(#name, zif_##alias, arg_info, 0)
^~~~
/usr/local/include/php/Zend/zend_API.h:77:74: note: in definition of macro 'ZEND_RAW_FENTRY'
#define ZEND_RAW_FENTRY(zend_name, name, arg_info, flags) { zend_name, name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags }, …
Run Code Online (Sandbox Code Playgroud) 在 PHP 8 中我们可以使用匹配表达式来代替 switch case。
如何为以下 switch case 正确编写匹配表达式?
switch($statusCode) {
case 200:
case 300:
$message = null;
break;
case 400:
$message = 'not found';
break;
case 500:
$message = 'server error';
break;
default:
$message = 'unknown status code';
break;
}
Run Code Online (Sandbox Code Playgroud)