Framework: Can't Console initalizing at php7.4 with Typed Properties

Created on 8 Jan 2020  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 6.10.0
  • PHP Version: 7.4.1

Description:

Hi, I'm laravel user, I'm try to adding artisan console with php 7.4, I encounter Some Error.

> In My Code

class test extends Command
{
    protected string $signature = 'test:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected string $description = 'test command for debug';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {

> Occured Error

$ php artisan

   Symfony\Component\Debug\Exception\FatalThrowableError  : Typed property Illuminate\Console\Command::$description must not be accessed before initialization

  at /var/www/foo/vendor/laravel/framework/src/Illuminate/Console/Command.php:118
    114|
    115|         // Once we have constructed the command, we'll set the description and other
    116|         // related properties of the command. If a signature wasn't used to build
    117|         // the command we'll set the arguments and the options on this command.
  > 118|         $this->setDescription((string) $this->description);
    119|
    120|         $this->setHelp((string) $this->help);
    121|
    122|         $this->setHidden($this->isHidden());

  Exception trace:

  1   Illuminate\Console\Command::__construct()
      /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/ClosureCommand.php:32

  2   Illuminate\Foundation\Console\ClosureCommand::__construct()
      /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:203

  Please use the argument -v to see more details.

I use php artisan make:command, and I setup command's signature and description.

Artisan make my custom command class with no typed properties. so I setup that.

(using command before properties type, it occuring error)

> In My Code

class test extends Command
{
    protected $signature = 'test:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test command for debug';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {

> Occured ERROR

$ php artisan

PHP Fatal error:  Type of App\Console\Commands\test::$signature must be string (as in class Illuminate\Console\Command) in /var/www/foo/app/Console/Commands/test.php on line 38
PHP Stack trace:
PHP   1. {main}() /var/www/foo/artisan:0
PHP   2. App\Console\Kernel->handle() /var/www/foo/artisan:37
PHP   3. App\Console\Kernel->bootstrap() /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:129
PHP   4. App\Console\Kernel->commands() /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:326
PHP   5. App\Console\Kernel->load() /var/www/foo/app/Console/Kernel.php:38
PHP   6. is_subclass_of() /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:239
PHP   7. spl_autoload_call() /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:239
PHP   8. Composer\Autoload\ClassLoader->loadClass() /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:239
PHP   9. Composer\Autoload\includeFile() /var/www/foo/vendor/composer/ClassLoader.php:322

   Symfony\Component\Debug\Exception\FatalErrorException  : Type of App\Console\Commands\test::$signature must be string (as in class Illuminate\Console\Command)

  at /var/www/foo/app/Console/Commands/test.php:38
    34|
    35|     public function handle() {
    36|
    37|     }
  > 38| }
    39|

  Exception trace:

  1   Symfony\Component\Debug\Exception\FatalErrorException::__construct()
      /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:148

  2   Illuminate\Foundation\Bootstrap\HandleExceptions::fatalExceptionFromError()
      /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:134

  Please use the argument -v to see more details.

   Whoops\Exception\ErrorException  : Type of App\Console\Commands\test::$signature must be string (as in class Illuminate\Console\Command)

  at /var/www/foo/app/Console/Commands/test.php:38
    34|
    35|     public function handle() {
    36|
    37|     }
  > 38| }
    39|

  Exception trace:

  1   Composer\Autoload\includeFile()
      /var/www/foo/vendor/composer/ClassLoader.php:322

  2   Composer\Autoload\ClassLoader::loadClass()
      /var/www/foo/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:239

  Please use the argument -v to see more details.

This is Only in My Develop environment Bug? or laravel is not to ready to use with php 7.4 yet?

I do not have to use php 7.4, so I will downgrade to php 7.3.

Most helpful comment

I found what is real problem.
I'm using phpstorm 2019.2. when I adding missing typed property in My Command class, IDE is Automatically adding type property at original Command class.

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected string $signature; # << Automatically  added typed

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name;

    /**
     * The console command description.
     *
     * @var string|null
     */
    protected string $description; # << Automatically  added typed

so, It effect artisan command run.
My problem solved by deleting added typed property at original Command class.
Thank you for answer!

All 4 comments

You don't need to add types to your properties in order to upgrade to php 7.4.

You can't add these types as the base class doesn't implement these. Since Laravel still needs to support PHP 7.2 and 7.3 we won't be adding these types ourselves anytime soon.

I found what is real problem.
I'm using phpstorm 2019.2. when I adding missing typed property in My Command class, IDE is Automatically adding type property at original Command class.

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected string $signature; # << Automatically  added typed

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name;

    /**
     * The console command description.
     *
     * @var string|null
     */
    protected string $description; # << Automatically  added typed

so, It effect artisan command run.
My problem solved by deleting added typed property at original Command class.
Thank you for answer!

I had this issue happen on PHPStorm 2020.1.1

i.e. the IDE is automatically adding the type properties to the base class when you use the IDE feature to add them to your class.

Was this page helpful?
0 / 5 - 0 ratings