我刚刚安装了 PHP 8,却出现此错误?我如何解决它?
致命错误: OM\Db::query(string $statement) 的声明必须与 /home/www/includes 中的 PDO::query(string $query, ?int $fetchMode = null, mix ...$fetchModeArgs) 兼容/OM/Db.php 第 131 行
我的 OM/Db.php
public function query(string $statement) =====> line 131
{
$statement = $this->autoPrefixTables($statement);
$args = func_get_args();
if (count($args) > 1) {
$DbStatement = call_user_func_array(array($this, 'parent::query'), $args);
} else {
$DbStatement = parent::query($statement);
}
if ($DbStatement !== false) {
$DbStatement->setQueryCall('query');
$DbStatement->setPDO($this);
}
return $DbStatement;
}
Run Code Online (Sandbox Code Playgroud) 尝试了很多方法,但仍然无法在 Docker 中运行的 PHP8 容器中启用 GD 和 JPEG 支持。这是我的 Docker 文件的片段:
FROM php:8.0.10-apache
RUN apt-get -y update && apt-get -y install \
apt-utils \
vim \
rsync \
curl \
openssl \
openssh-server \
mariadb-client \
git \
zlib1g-dev \
libicu-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libzip-dev \
libpng-dev \
g++ \
zip \
unzip \
gnupg \
gnupg2 \
unixodbc-dev
RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
Run Code Online (Sandbox Code Playgroud)
如您所见,缺少 JPEG 支持。我在这里缺少什么?谢谢!
Eloquent 允许Enum Casting。
Eloquent 还允许您将属性值转换为 PHP 枚举。为此,您可以在模型的 $casts 属性数组中指定要转换的属性和枚举:
Run Code Online (Sandbox Code Playgroud)use App\Enums\ServerStatus; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'status' => ServerStatus::class, ];一旦在模型上定义了转换,当您与属性交互时,指定的属性将自动转换为枚举或从枚举转换:
Run Code Online (Sandbox Code Playgroud)if ($server->status == ServerStatus::provisioned) { $server->status = ServerStatus::ready; $server->save(); }
是否可以在 eloquent 中对数组值使用枚举转换?
我有一个现有的雄辩模型,用于具有一种类型。现在它需要支持多个。
Laravel 是否支持数组枚举转换或者这不可能?实现类似目标的替代方法是什么?
PHP 8 改变了 ZIP 存档的“打开”方式,值得注意的是:
不推荐使用空文件作为 ZipArchive。Libzip 1.6.0 不再接受空文件作为有效的 zip 存档。
在下面的测试代码中,打开名为 的 ZIP 文件$backupzip可以正常工作,但打开 ZIP 文件名$invoicezip失败并出现错误:
已弃用:ZipArchive::open():使用空文件,因为 ZipArchive 在第 12 行已弃用
<?php
declare(strict_types=1);
ini_set('display_errors','1');ini_set('display_startup_errors','1');error_reporting(E_ALL);
define('BACKUPDIR','E:\Database_Backups\\');
$backupfile = BACKUPDIR . date('Ymd') . '.zip';
$temp_file = tempnam(sys_get_temp_dir(),'AW');
$backupzip = new ZipArchive();
$invoicezip = new ZipArchive();
$backupzip->open($backupfile,ZipArchive::CREATE); // <<<--- this works
$invoicezip->open($temp_file,ZipArchive::CREATE); // <<<--- this fails
Run Code Online (Sandbox Code Playgroud) 当我尝试安装任何主题到 WordPress 时出现此错误,我该怎么办?
Fatal error: Uncaught ArgumentCountError: array_merge() does not
accept unknown named parameters in
D:\xampp\htdocs\wordpress\wp-includes\widgets.php:1261
Stack trace:
#0
D:\xampp\htdocs\wordpress\wp-includes\widgets.php(1261):
array_merge(wp_inactive_widgets: Array, sidebar-1: Array, sidebar-2:
Array, sidebar-3: Array)
#1
D:\xampp\htdocs\wordpress\wp-includes\widgets.php(1216):
retrieve_widgets(true)
#2
D:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(289):
_wp_sidebars_changed('Twenty Twenty')
#3 D:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(311):
WP_Hook->apply_filters(NULL, Array)
#4
D:\xampp\htdocs\wordpress\wp-includes\plugin.php(478):
WP_Hook->do_action(Array)
#5
D:\xampp\htdocs\wordpress\wp-includes\theme.php(2974):
do_action('after_switch_th...', 'Twenty Twenty', Object(WP_Theme))
#6
D:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(287):
check_theme_switched('')
#7
D:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(311):
WP_Hook->apply_filters(NULL, Array)
#8
D:\xampp\htdocs\wordpress\wp-includes\plugin.php(478):
WP_Hook->do_action(Array)
#9
D:\xampp\htdocs\wordpress\wp-settings.php(540): do_action('init')
#10
D:\xampp\htdocs\wordpress\wp-config.php(90):
require_once('D:\\xampp\\htdocs...')
#11
D:\xampp\htdocs\wordpress\wp-load.php(37):
require_once('D:\\xampp\\htdocs...')
#12
D:\xampp\htdocs\wordpress\wp-blog-header.php(13):
require_once('D:\\xampp\\htdocs...')
#13
D:\xampp\htdocs\wordpress\index.php(17):
require('D:\\xampp\\htdocs...')
#14 {main} thrown in
D:\xampp\htdocs\wordpress\wp-includes\widgets.php on line …Run Code Online (Sandbox Code Playgroud) 我定义一个类如下,使用https://blog.jetbrains.com/phpstorm/2020/10/phpstorm-2020-3-eap-4/#arrayshape的建议将数组形状定义为常量,以便它可以在项目中使用:
<?php
namespace my\namespace\Api;
use JetBrains\PhpStorm\ArrayShape;
class CheckoutSessionLineItemMetadata implements MetadataInterface {
/**
* The ArrayShape of toArray().
*/
public const ARRAY_SHAPE = [
'order_item_id' => "string",
'order_id' => "string",
'product_variation_id' => "string",
];
/**
* Constructs a new object.
*
* @param string $order_item_id
* @param string $order_id
* @param string $product_variation_id
*/
public function __construct(
private string $order_item_id,
private string $order_id,
private string $product_variation_id) {
}
/**
* {@inheritDoc}
*/
#[ArrayShape(self::ARRAY_SHAPE)] public function toArray(): array {
return …Run Code Online (Sandbox Code Playgroud) 有没有办法使用反射或其他方式来更改已设置的只读属性?
我们有时会在测试中这样做,并且我们不想避免仅出于测试目的而使用只读属性。
class Acme {
public function __construct(
private readonly int $changeMe,
) {}
}
$object= new Acme(1);
$reflectionClass = new ReflectionClass($object);
$reflectionProperty = $reflectionClass->getProperty('changeMe');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, 2);
Run Code Online (Sandbox Code Playgroud)
Fatal error: Uncaught Error: Cannot modify readonly property Acme::$changeMe
Run Code Online (Sandbox Code Playgroud) 我有一个仅适用于两种类型a和 的课程b。
我今天的“旧式”代码:
class Work1 {
public function do(string $type):string {
if ($type!="a" && $type!="b")
throw new Error("wrong type");
return "type is $type";
}
}
echo (new Work())->do("a"); // type is a
echo (new Work())->do("c"); // exception: wrong type
Run Code Online (Sandbox Code Playgroud)
现在有了 PHP 8,我们有了enum更好的参数检查选项:
enum WorkType {
case A;
case B;
}
class Work2 {
public function __construct(private WorkType $type) {}
public function do() {
return "type is ".$this->type->name;
}
}
echo (new Work2(WorkType::A))->do(); // type …Run Code Online (Sandbox Code Playgroud) 让 PHP7 和 PHP 8 共存
我已经从 PHP 7 升级到 PHP 8。正如 PHP 的通常情况一样,我仍然有 php7.4 /usr/bin(以及 php8.0)。但是,当我运行该php -v命令时,它回答 php8.0。
从那时起,当尝试安装软件(例如 Docker 和 VirtualBox)时,我收到错误消息php7.4-fpm.service: Failed with result 'exit-code',后跟Failed to start The PHP 7.4 FastCGI Process Manager. 因此,我似乎需要 php7.4 来运行才能安装此类应用程序。
但我使用 PHP 8 功能来编写网站,因此不想回到 php7.4
有没有办法同时使用php8.0和php7.4?我可以让 PHP 8 在虚拟机上运行,但它相当繁重。
我使用的是 Ubuntu 20(但我不确定这个问题是否特定于操作系统)。
兹洛茨
这是我想使用的:
#[ORM\Column(type="string")]
Run Code Online (Sandbox Code Playgroud)
而不是这个:
/**
* @ORM\Column(type="string")
*/
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
(error: Class 'Column' is not annotated with 'Attribute' )
Run Code Online (Sandbox Code Playgroud)
是因为 Doctrine 还不支持它,还是我错过了什么?