Bug #80938 [Com]: ParseError with class_exists & property promotion

From: Date: Mon, 05 Apr 2021 16:23:27 +0000
Subject: Bug #80938 [Com]: ParseError with class_exists & property promotion
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233184@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80938&edit=1

 ID:                 80938
 Comment by:         danack@php.net
 Reported by:        jr at rushlow dot dev
 Summary:            ParseError with class_exists & property promotion
 Status:             Not a bug
 Type:               Bug
 Package:            Class/Object related
 PHP Version:        7.4.16
 Block user comment: N
 Private report:     N

 New Comment:

https://3v4l.org/oEW4q even


Previous Comments:
------------------------------------------------------------------------
[2021-04-05 16:22:56] jr at rushlow dot dev

That makes sense. But to put it another way for clarity - calling class_exists() on a class that is
not correctly defined for the PHP version used a runtime cannot be done?

If that is the case - should we change the docs to reflect this? 

"class_exists — Checks if the class has been defined" -> https://www.php.net/manual/en/function.class-exists.php

I'll start a thread in internals (to further discuss options) if the above is true as it would
seem that one can no longer use class_exists() in code bases that support multiple PHP versions.
Granted I may be missing something obvious :)

------------------------------------------------------------------------
[2021-04-05 16:22:25] danack@php.net

Incidentally, the reproduce code gives a different error https://3v4l.org/oEW4q:

Fatal error: Uncaught Error: Undefined constant "PromoteConstructorProperty" in
/in/oEW4q:11

class_exists(PromoteConstructorProperty); 

should probably be:

class_exists(PromoteConstructorProperty::class);

------------------------------------------------------------------------
[2021-04-05 16:05:30] nikic@php.net

class_exists() with autoload:true (the default) will load the class. If the class declaration
doesn't parse, you get an error, as usual.

------------------------------------------------------------------------
[2021-04-05 16:03:13] jr at rushlow dot dev

Description:
------------
Calling class_exists() on a class that uses constructor property promotion results in a ParseError.

Property promotion is only available in PHP8.0+ but one would expect that you could use
class_exists() regardless class implementation.

Test script:
---------------
// See https://github.com/rushlow-development/attribute-test
for a simple reproducer w/ composer && phpunit

<?php

class PromoteConstructorProperty
{
    public function __construct(
        public string $var
    ) {
    }
}

$bool = class_exists(PromoteConstructorProperty);

Expected result:
----------------
$bool = true

Actual result:
--------------
ParseError: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80938&edit=1


Thread (8 messages)

« previous php.bugs (#233184) next »