magento2: PHP7 return types incompatibile with generate code
Preconditions
- PHP 7.0.8 (any compatible PHP 7 release should be sufficient though)
Steps to reproduce
- Blank install
- Create a new block and add a method with a return type in PHP7 format e.g
: string
- Use the block in some layout somewhere
- Hit the frontend to use the block
Expected result
- The page should load and the block code be executed correctly
Actual result
Instead you get a fatal error from PHP because the generated interceptor class doesn’t have a return type at all.
PHP Fatal error: Declaration of XX\YY\Block\CustomBlock\Interceptor::someMethod() must be compatible with XX\YY\Block\CustomBlock\::someMethod(): bool in .../var/generation/XX/YY/Block/CustomBlock/Interceptor.php on line X
This of course can be a tricky one to resolve purely based on the fact you wouldn’t want to break BC with PHP 5.X.
The only resolutions I can think of at this point is…
- Check the version at runtime
- Set the PHP version (like we set the deploy mode)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 10
- Comments: 27 (18 by maintainers)
Commits related to this issue
- width and height allow null types and remove return types as they are still not supported by magento https://github.com/magento/magento2/issues/6106 — committed to staempfli/magento2-module-image-resizer by jalogut 7 years ago
- MAGETWO-59661: [Github] PHP7 returns types incompatible with generate code #6106 - Updated Zend Code library to 3.x version and it dependencies - Refactored code generators to support PHP 7.x featu... — committed to magento/magento2 by joni-jones 7 years ago
- MAGETWO-59661: [Github] PHP7 returns types incompatible with generate code #6106 - Updated reference blacklist - Added method description for event listener — committed to magento/magento2 by joni-jones 7 years ago
- MAGETWO-59661: [Github] PHP7 returns types incompatible with generate code #6106 - Reverted blacklist changes — committed to magento/magento2 by joni-jones 7 years ago
- MAGETWO-59661: [Github] PHP7 returns types incompatible with generate code #6106 - Resolved composer conflicts — committed to magento/magento2 by joni-jones 7 years ago
- MAGETWO-59661: [Github] PHP7 returns types incompatible with generate code #6106 - Resolved merge conflicts — committed to magento/magento2 by joni-jones 7 years ago
I use Magento v2.3.2 I need to enable MAGE_PROFILER=2, but got errors like this:
It looks like the same problem but with loggers generation in this time. How can I fix it?
It seems the version of
zendframework/zend-code
Magento 2 ships with does not support return type hints in code generation, nor is it easy to update the version ofzendframework/zend-code
due to conflicting dependencies.We decided to temporarily patch this and bring in some
zendframework/zend-code
features to add return type hints. Here is a patch if anyone else wants this feature: https://gist.github.com/AydinHassan/064f4bbd33fc118f9fa655811df6a660.Note that you will need to put it in your own module and rename the namespaces.
That sounds way better) Thanks for your efforts.