我刚收到这个致命的错误
可捕获的致命错误:传递给File::__construct()的参数1必须是整数的实例,给定整数,在第9行的/home/radu/php_projects/audio_player/php/File.php中调用,在第7行的/home/radu/php_projects/audio_player/php/File.php中定义
所以,这是一门课
class File{
public $id;
public $name;
public $file_paths;
public funct
我一直试图在interface中指定函数的返回类型,是PHP缺乏对此的支持,还是缺少了什么?
<?php interface AInterface { public function F() : self; }
当我实现上面的interface时,以下内容会导致与不匹配声明有关的致命错误:
<?php class A { public function F() : self { return $this; } }
编辑:我知道删除: self中的interface修复了错误,但这是否意味着无法使用接口确保返回类型?
Comparare.php
<?php
interface Comparare{
public function compara(self $a);
}
?>
clasa.php
<?php
class Clasa implements Comparare{
public $v;
public function compara(self $a){
if($this->v < $a->v)
{
return -1;
}
我在用PHP 7测试返回类型。
我创建了一个简单的脚本来测试PHP 7的返回类型:
<?php
Class Obj {
public function __construct(){
}
public function test(): string { //a string needs to be returned
return "ok";
}
}
function foo(): Obj { //instance of Obj needs to be returned
return new Obj();
}
当生成解析错误(例如在Controller内部)时,ZF2异常处理程序会捕获它,我只看到
An error occurred during execution; please try again later.
No exception available
如果我在public/index.php中生成一个解析错误,我可以在浏览器中看到简单的PHP错误,所以PHP设置是正确的。
如何强制ZF2在dev模式下显示这样的错误?
我的代码在这里
<?php
select("id","name");
function select(string ...$value){
var_dump($value);
}
但它只能在php 7上运行。当我在PHP5.6机器上工作时,我得到了像这样的致命错误。
Catchable fatal error: Argument 1 passed to select() must be an instance of string, string given, called in [
我发现PHP在重写子类中的方法时,使用子类型作为方法的参数,这似乎是不一致的。如果我用代码解释,这是最简单的:
interface IngredientInterface {}
interface TomatoInterface extends IngredientInterface {}
class Tomato implements TomatoInterface {}
class Soup {
public function __construct(IngredientInterface $ingredient) {}
public function foo(Ingr
我有一个返回true或false的方法,但是当我添加类型声明isAdmin() : string时,它不会抛出致命错误。
我在类之前包含的第二个文件中包含了declare(strict_types=1);。示例:
file1.php
<?php
declare(strict_types=1);
file2.php
<?php
include "file1.php";
class Test
{
public function user() : int
{
return true;
}
}
$test = new
我的项目在PHP7.x上运行良好,在升级到PHP8之后,我遇到了以下问题,我不知道如何解决这个问题。 我有以下(简化的)情况: <?php
class Vehicle
{
//...
}
class Car extends Vehicle
{
//...
}
class VehicleOutputMaker
{
public function output(Vehicle $entity)
{
}
}
class CarOutputMaker extends VehicleOutputMaker
{
//TH
我试图在php联机编译器上运行一个非常简单的代码,但是每次都会出现错误:下面是
<?php
function value(integer $h) {
if(!is_int($h)) {
throw new InvalidArgumentException('integer expected');
}
if($h <= 7) {
throw new RangeException('integer should be greater than 7');
}
echo $h;
最近,我读了几篇关于PHP面向对象编程的文章,遇到了这样的情况:
class a {}
$temp = new a();
$temp->newfiled = 20; // This field is not present within the class
以上代码在PHP5.3中运行良好。
为什么剂量PHP抛出一个错误?我来自Java背景,在这个背景中,我们只能使用在类中定义的字段。
更新:编辑了php.ini到error_reporting = E_ALL = E_STRICT,但是它继续隐藏错误。
也是的一个特性吗?
OS: macOS Big SurPHP版本:7.4.1包名称和版本: google/apiclient:^2.12.3
每当我将PHP版本切换到7.4.1时,我就会得到这个错误
Parse error: syntax error, unexpected 'static' (T_STATIC) in /Users/webtechstreet4/Local Sites/fvtest/app/public/wp-content/plugins/form-vibes-pro/vendor/psr/cache/src/CacheItemInterface.php on line 75
在第31页上的示例
class Foo<T> {
public function add(T $delta): Foo {
$this->num += $delta; // line 6
return $this;
}
public function get(): T {
return $this->num;
}
public function __construct(private T $num): void {}
}
$f1 = new Foo(123)
据我所知,php有能力防止返回类型在它知道有问题的地方被声明。
class Foo {
public function __clone(): Baz {
return new Baz;
}
}
class Baz {
}
$foo = new Foo;
$newFoo = clone $foo;
这导致了一个Fatal error: Clone method Foo::__clone() cannot declare a return type,这是完全合理的。
但是为什么php会允许这样的东西:
class Foo {
public functi
考虑下面的代码片段:
class Foo
{
public function fooMethod()
{
return [];
}
}
class Bar extends Foo
{
public function fooMethod(): array
{
return ['something'];
}
}
这没有任何错误(在PHP7.4和PHP7.3中进行了测试)。为什么PHP不强制使子对象的方法签名与父对象相同(即没有返回类型)?PHP允许这样做有什么合理的理由吗?还是说这是一个bug?我的猜