CodeIgniter Forums
Using ::class in Model's $returnType instead of string - bad? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Using ::class in Model's $returnType instead of string - bad? (/showthread.php?tid=82127)



Using ::class in Model's $returnType instead of string - bad? - tgix - 06-15-2022

Before I go abound and create a PR for a documentation change, I have to ask if I have misunderstood something.
I am refactoring my CI 4.1.9 project in PHPstorm and found out that
PHP Code:
protected $returnType App\Entities\SomeEntity::class; 
 is an excellent way of writing this instead of specifying this as a string 
PHP Code:
protected $returnType 'App\Entities\SomeEntity'

As I read it from the documentation, "::class" expands at compile time so it can't be for performance reasons - https://www.php.net/manual/en/language.oop5.basic.php
What am I missing?
/Mattias


RE: Using ::class in Model's $returnType instead of string - bad? - kenjis - 06-15-2022

Yes, "::class" is better.
If you are using an IDE, the IDE auto completes class names to avoid typos,
and you can go to the class file with one click.


RE: Using ::class in Model's $returnType instead of string - bad? - paulbalandan - 06-15-2022

Actually, the Model Generator creates the return types using the "::class" syntax:
PHP Code:
$return '\\' trim($return'\\') . '::class';
$this->call('make:entity'array_merge([$baseClass], $this->params)); 

See https://github.com/codeigniter4/CodeIgniter4/blob/ed22aa5ce674dbf74b5c49fb4eee6f3559d6b026/system/Commands/Generators/ModelGenerator.php#L126-L127


RE: Using ::class in Model's $returnType instead of string - bad? - tgix - 06-15-2022

So maybe then the docs should be updated to reflect "best practices"?


RE: Using ::class in Model's $returnType instead of string - bad? - kenjis - 06-16-2022

(06-15-2022, 11:12 PM)tgix Wrote: So maybe then the docs should be updated to reflect "best practices"?

Yes.


RE: Using ::class in Model's $returnType instead of string - bad? - tgix - 06-16-2022

(06-16-2022, 02:12 AM)kenjis Wrote:
(06-15-2022, 11:12 PM)tgix Wrote: So maybe then the docs should be updated to reflect "best practices"?

Yes.

Working on a PR.

https://github.com/codeigniter4/CodeIgniter4/pull/6134


RE: Using ::class in Model's $returnType instead of string - bad? - MGatner - 06-16-2022

Thanks for the discussion and contribution! I use ::class any time I have a class string. You can also use the special phpdoc block type `class-string` to narrow types for IDE and static analysis (e.g. https://phpstan.org/writing-php-code/phpdoc-types#class-string)


RE: Using ::class in Model's $returnType instead of string - bad? - tgix - 06-16-2022

(06-16-2022, 04:35 AM)MGatner Wrote: Thanks for the discussion and contribution! I use ::class any time I have a class string. You can also use the special phpdoc block type `class-string` to narrow types for IDE and static analysis (e.g. https://phpstan.org/writing-php-code/phpdoc-types#class-string)

Cool. phpstan is next on my to-do-list to start using.