为了保持一致性,我从 PHP 7.1 开始为所有方法指定返回类型,包括像 这样的魔术方法__toString,甚至当隐式返回类型类似于voidwith时__unserialize():
class a {
function __toString() : string {}
function __unserialize ( array $data ) : void {}
function __wakeup() : void {}
}
Run Code Online (Sandbox Code Playgroud)
当我对构造函数和析构函数尝试相同时,如下所示:
class a {
function __construct() : void {}
function __destruct() : void {}
function __clone() : void {}
}
Run Code Online (Sandbox Code Playgroud)
PHP 产生Fatal errors:
Constructor a::__construct() cannot declare a return type
Destructor a::__destruct() cannot declare a return type
Clone method a::__clone() cannot …Run Code Online (Sandbox Code Playgroud) 我在 Ubuntu 上使用最新版本的 Visual Studio Code (1.52.1) 和 PHP Intelephense 1.5.4。尽管它是最新版本,但它似乎不知道新的 PHP 8 语法。例如,它在使用 nullsafe 运算符时显示错误:
$myobject?->myfunction();
Run Code Online (Sandbox Code Playgroud)
有没有办法教 VSC PHP 8 还是我们必须等待更新?
编码
<?php
$consts = get_defined_constants();
$consts = array_keys($consts);
usort($consts,function($a,$b){return (int)(strlen($a)<strlen($b));});
foreach($consts as $const){
echo strlen($const).": ".$const."\n";
}
Run Code Online (Sandbox Code Playgroud)
将在 PHP 8.0.0 之前按照我的预期从最长到最短打印所有定义的常量。7.3.13 开始于
62: SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE
62: SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE
60: SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE
60: SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE
51: SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX
Run Code Online (Sandbox Code Playgroud)
但我不知道 PHP8.0.0 做了什么,它的输出开始于:
9: E_WARNING
21: FILTER_FLAG_STRIP_LOW
7: E_ERROR
26: FILTER_FLAG_STRIP_BACKTICK
Run Code Online (Sandbox Code Playgroud)
你可以在 3v4l 上看到它:https ://3v4l.org/MP2IF
那么在 PHP 8.0.0 中发生了什么来破坏这段代码呢?
我在 ubuntu 服务器 20.04 上测试了 phpmyadmin 5.0.4 和 5.1.0.RC1,出现以下错误:
Warning in ./libraries/classes/Config.php#1285
mkdir(): Permission denied
Backtrace
./libraries/classes/Config.php#1285: mkdir(
string '/usr/share/phpmyadmin//var/lib/phpmyadmin/tmp/twig',
integer 504,
boolean true,
)
./libraries/classes/Template.php#57: PhpMyAdmin\Config->getTempDir(string 'twig')
./libraries/classes/Theme.php#101: PhpMyAdmin\Template->__construct()
./libraries/classes/Theme.php#174: PhpMyAdmin\Theme->__construct()
./libraries/classes/ThemeManager.php#307: PhpMyAdmin\Theme::load(
string './themes/pmahomme',
string '/usr/share/phpmyadmin/./themes/pmahomme/',
)
./libraries/classes/ThemeManager.php#79: PhpMyAdmin\ThemeManager->loadThemes()
./libraries/classes/ThemeManager.php#121: PhpMyAdmin\ThemeManager->__construct()
./libraries/classes/ThemeManager.php#385: PhpMyAdmin\ThemeManager::getInstance()
./libraries/common.inc.php#232: PhpMyAdmin\ThemeManager::initializeTheme()
./index.php#15: require_once(./libraries/common.inc.php)
Run Code Online (Sandbox Code Playgroud)
我尝试通过 www-data(原始“root”)更改我的 ./libraries/classes/Config.php 和 /var/lib/phpmyadmin/tmp/twig(也创建了此文件夹“twig”)的所有者,但是错误没有解决。
我的供应商配置是:
<?php
/**
* File for vendor customization, you can change here paths or some behaviour,
* which vendors such as Linux distributions might want to …Run Code Online (Sandbox Code Playgroud) 我有一个属性如下:
#[Attribute]
class State{
public function __constractor(
public string $name
){}
}
Run Code Online (Sandbox Code Playgroud)
我想将多个状态添加到我的类中,如下所示:
#[
State('a'),
State('b')
]
class StateMachine{}
Run Code Online (Sandbox Code Playgroud)
一切都很好,我可以访问属性列表,如下所示:
$attrs = $classReflection->getAttributes(State::class);
Run Code Online (Sandbox Code Playgroud)
但问题是,每当我尝试即时其中一个时,都会引发错误:
$instance = $attrs[0]->newInstance();
Run Code Online (Sandbox Code Playgroud)
错误是:
Error: Attribute "State" must not be repeated
Run Code Online (Sandbox Code Playgroud)
任何想法?
我已经安装了 PHP 版本 8.0.8 并按照本教程在 Wamp Server 中添加新的 PHP 版本。
该教程在文件中说wampserver.conf,LoadModuleFile是php8apache2_4.dll. exceptphp8apache2_4.dll在我的 PHP 8 存储库中不存在。我在哪里可以找到/下载它?
我有一个页面正在尝试将注释转换为 PHP8 属性。
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[IsGranted('ROLE_ADMIN')]
#[Route('/page')]
class PageController extends AbstractController
{
#[Route('/', name: 'page')]
public function index(): Response
{
return $this->render('page/index.html.twig', [
'controller_name' => 'PageController',
]);
}
}
Run Code Online (Sandbox Code Playgroud)
该IsGranted属性似乎不起作用,因为页面可以访问,而不是 403 错误。另一方面,当转换为注释时,如下所示,它会按预期工作。是否有我缺少的配置设置?
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/page')]
class PageController extends AbstractController
{
#[Route('/', name: 'page')]
public function index(): Response
{
return $this->render('page/index.html.twig', [
'controller_name' => 'PageController',
]);
}
}
Run Code Online (Sandbox Code Playgroud)
其他属性(例如#[Route]等#[Entity])有效,但安全属性似乎不起作用。
所以我有一个以前工作正常的插件,但几天后它向我抛出一个错误:
PHP 致命错误:未捕获错误:调用未定义的函数 create_function()
经过一番搜索,我发现这是因为create_function()PHP 8 中已弃用。
现在导致问题的确切行是这样的:
$callback_2 = create_function('$matches', 'return "[" . str_replace("|", "", $matches[1]) . "]";');
Run Code Online (Sandbox Code Playgroud)
我尝试将其更改为:
$callback_2 = create_function('$matches', 'return "[" . str_replace("|", "", $matches[1]) . "]";');
Run Code Online (Sandbox Code Playgroud)
但这不起作用。因此,如果有人能指出我正确的方向,而且我对 PHP 很陌生,那就太好了。
我正在将 php 7 升级到 php 8.0。在以前的 php 版本 7 中,这段代码工作正常。
$child_parent['parent'][$resultData->parent_id]->child = 'Yes';
Run Code Online (Sandbox Code Playgroud)
上面的代码在 php 7 中工作正常。现在我正在升级 php 版本 8.0。在这个新版本中它给出了这个错误。
遇到未捕获的异常类型:错误
消息:尝试将属性“child”分配给 null
Foo是具有特定方法的基类。大多数这些方法使用相同的类型(例如Foo::setNext(self $foo):)。
我想创建扩展的类Foo,并且只允许使用与它们本身严格相同的类型(Extend1Foo类型的对象不能与Extend2Foo类型的对象一起使用)。
在以下代码中,由于返回类型为 static,getBar()因此引发错误。这就是我想要的。但是,setBar()由于self参数类型,允许接收 Foo 的任何实例作为参数。
可重现的例子:
class Foo
{
private ?self $bar = null;
public function getBar(): static {
return $this->bar;
}
public function setBar(self $object): void {
$this->bar = $object;
}
}
class Foo1 extends Foo { /* specific methods */ }
class Foo2 extends Foo { /* specific methods */ }
$foo1 = new Foo1;
$foo1->setBar(new Foo2); // <<< No TypeError, but I want it. …Run Code Online (Sandbox Code Playgroud) php ×10
php-8 ×10
annotations ×1
apache ×1
covariance ×1
intelephense ×1
mkdir ×1
oop ×1
php-7 ×1
php-7.1 ×1
phpmyadmin ×1
symfony ×1
symfony5 ×1
typeerror ×1
types ×1
usort ×1
wampserver ×1
wordpress ×1