Using ::class in Model's $returnType instead of string - bad? |
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; 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
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.
Actually, the Model Generator creates the return types using the "::class" syntax:
PHP Code: $return = '\\' . trim($return, '\\') . '::class'; See https://github.com/codeigniter4/CodeIgni...#L126-L127 (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"? Working on a PR. https://github.com/codeigniter4/CodeIgniter4/pull/6134
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/php...ass-string)
(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/php...ass-string) Cool. phpstan is next on my to-do-list to start using. |
Welcome Guest, Not a member yet? Register Sign In |