[php-src] Issue #9221: Defer loading `use` classes into memory (helps support ::class, infrequently used code paths, etc)
| From: | garygreen | Date: | Mon, 01 Aug 2022 20:14:37 +0000 |
| Subject: | [php-src] Issue #9221: Defer loading `use` classes into memory (helps support ::class, infrequently used code paths, etc) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242098@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9221
Author: garygreen
### Description
Currently when adding a
use class to a PHP file it will cause that class to be imported
into memory, even if it's not actually subsequently used in the class.
For example:
```php
<?php
use App\Support;
```
Lots of developers may do this in order to just use Support::class shortcut without
realising the use statement loads all those classes into memory.
Is there a way for PHP to know that file wasn't *actually* used, so doesn't need to load
it into memory? If that's not easily possible, maybe this could be hinted through an attribute?
```php
#[\DeferUse]
use App\Support;
```
I would hope PHP can just defer loading the class into memory straight away for all use
though, until it's actually needed. That would have a huge impact on all PHP apps reducing
memory usage.