Skip to content

[PHP 8] Switch with get_class($object) works while $object::class doesn't #4896

@ruudk

Description

@ruudk

Bug report

<?php declare(strict_types = 1);

/** @var DateTime|DateInterval $command */
$command = new DateTime();

switch ($command::class) {
	case DateTime::class:
		// Call to an undefined method DateInterval\|DateTime::getTimestamp().
                var_dump($command->getTimestamp());
		break;
	case DateInterval::class:
		echo " Hello Date Interval " . $command->format('d');

		break;
}

switch (get_class($command)) {
	case DateTime::class:
                // Works
		var_dump($command->getTimestamp());
		break;
	case DateInterval::class:
		echo " Hello Date Interval " . $command->format('d');

		break;
}

Code snippet that reproduces the problem

https://phpstan.org/r/7edb1da5-a99e-4788-8e1e-08dbf783ea13

https://3v4l.org/MdXXb

Expected output

No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions