Skip to content

Incorrect Declaration .. must be compatible with ... #890

Description

@Disservin

The following generates an error from devsense

<?php

require_once 'vendor/autoload.php';

use Carbon\Carbon as BaseCarbon;

class MyCarbon extends BaseCarbon {
    public static function createFromFormat($format, $time, $timezone = null): ?static
    {
        return parent::createFromFormat($format, $time, $timezone);
    }
}

Error:

Declaration of MyCarbon::createFromFormat($format, $time, $timezone = null): static must be compatible with Carbon::createFromFormat($format, $time, $timezone = null): mixed|null

however, this error seems to be incorrect, both php index.php show no error as well as PHP Intelephense from Ben Mewburn.

Changing the return type to an incorrect one produces a real php error, i.e. : ?int

Fatal error: Declaration of MyCarbon::createFromFormat($format, $time, $timezone = null): ?int must be compatible with Carbon\Carbon::createFromFormat($format, $time, $timezone = null): ?Carbon\Carbon in index.php on line 8

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions