Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7 Fatal error: static::class cannot be used for compile-time class name resolution

Tags:

php

php-7

Current Travis-CI PHP7 builds throw the following error when executing the following code:

PHP 7 Fatal error: static::class cannot be used for compile-time class name resolution

trait EloquentValidatingTrait
{
    // Some declarations skipped
/**
 * Eloquent will call this on model boot
 */
public static function bootEloquentValidatingTrait()
{
    // Calling Model::saving() and asking it to execute assertIsValid() before model is saved into database
    $savingCallable = [static::class, 'saving'];
    $validationCallable = [static::class, 'assertIsValid'];
    forward_static_call($savingCallable, $validationCallable);
}

Is that a temporary bug or a future feature I missed? Notes below this RFC says it should work (and it does in 5.5 and 5.6).

like image 658
Vladislav Rastrusny Avatar asked Jun 03 '15 20:06

Vladislav Rastrusny


People also ask

What does :: class do in PHP?

SomeClass::class will return the fully qualified name of SomeClass including the namespace. This feature was implemented in PHP 5.5. It's very useful for 2 reasons. You can use the use keyword to resolve your class and you don't need to write the full class name.

How to create class instance in PHP?

To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).

Which keyword is used to create an object from a class in PHP?

Definition and UsageThe new keyword is used to create an object from a class.


1 Answers

Fixed this bug via http://git.php.net/?p=php-src.git;a=commitdiff;h=1d3f77d13d2b457bdf1bc52045da4679741e65cb

The mistake was simple... I had in compile time constant resolution optimization set the mode to force succeed or die (a simple boolean to a function call). That mode is needed for static expressions (like const FOO = static::class; must fail).

Set that to zero and now it works fine. Just pull the newest master for a fix.

like image 183
bwoebi Avatar answered Sep 28 '22 18:09

bwoebi