Skip to content

Class constants allow narrowing override from untyped to typed #19049

Closed as not planned
@khaledalam

Description

@khaledalam

Description

Ref: RFC: typed_class_constants

The following code https://3v4l.org/MUm1l:

<?php

// overriding an untyped parent constant with a typed one

class A {
    public const VALUE = 42; // untyped
}

class B extends A {
    public const float VALUE = 42.5; // narrowing to `float`
}

echo "Parent type: " . gettype(A::VALUE) . "\n";
echo "Child value: " . B::VALUE . "\n";

Resulted in this output:

Parent type: integer
Child value: 42.5

But I expected this output instead:

Fatal error: Cannot override constant A::VALUE with incompatible type in %s on line %d

PHP Version

8.3.0 - 8.3.23, 8.4.1 - 8.4.10

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions