Magento2: [Issue] Fixed 'Undefined class constant' error when interceptor is generated.

Created on 3 Jul 2020  路  14Comments  路  Source: magento/magento2

This issue is automatically created based on existing pull request: magento/magento2#28797: Fixed 'Undefined class constant' error when interceptor is generated.


Summary (*)

  1. Magento 2.4-develop
  2. Since constant is declared as private it's not accessible from parent classes.
  3. Since it used with static keyword, PHP will search it within class instance.
  4. Class instance will be an instance of generated Interceptor without access to private variables of child classes.
  5. This will lead to an 'Undefined class constant' error.

Examples (*)

  1. Create a plugin for Magento\Theme\Block\Html\Title class:

    <type name="Magento\Theme\Block\Html\Title">
        <plugin name="titlePlugin" type="Magento\Theme\Plugin\TitlePlugin" />
    </type>
    
    <?php
    
    namespace Magento\Theme\Plugin;
    
    class TitlePlugin
    {
        /**
         * @param mixed $subject
         * @param mixed $result
         * @return mixed
         */
        public function afterGetPageHeading(
            $subject,
            $result
        ) {
            return $result;
        }
    }
    
  2. Open frontend.

  3. You'll see an error:

    Error: Undefined class constant 'XML_PATH_HEADER_TRANSLATE_TITLE' in app/code/Magento/Theme/Block/Html/Title.php:104 
    Stack trace: 
    #0 app/code/Magento/Theme/Block/Html/Title.php(82): Magento\Theme\Block\Html\Title->shouldTranslateTitle() 
    #1 generated/code/Magento/Theme/Block/Html/Title/Interceptor.php(32): Magento\Theme\Block\Html\Title->getPageHeading() 
    #2 app/code/Magento/Theme/view/frontend/templates/html/title.phtml(12): Magento\Theme\Block\Html\Title\Interceptor->getPageHeading()
    ...
    

Proposed solution

Another way to solve this bug - is to declare constant as public instead of private.
If you think it should be public - I'll update PR.

Theme Fixed in 2.4.x Confirmed Format is valid Ready for Work P4 done Reported on 2.4.0 Reproduced on 2.4.x S3 Done

Most helpful comment

I don't understand how 2.4.0 could be released with this error in it which basically just crashes every frontend page.

All 14 comments

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @engcom-Delta
Thank you for verifying the issue. Based on the provided information internal tickets MC-35619 were created

Issue Available: @engcom-Delta, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

we have the same issue. Please fix it

Hi @m2-assistant[bot]. Thank you for your report.
The issue has been fixed in magento/magento2#28797 by @vovayatsyuk in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.1 release.

@magento-engcom-team why fatal error will be included only in 2.4.1 version? This is a critical error. Is any chance to include this fix into 2.4.0 version? Safe fix

I don't understand how 2.4.0 could be released with this error in it which basically just crashes every frontend page.

@Hexmage we have created patch - mini module to resolve this error. Maybe it will be useful for you
https://amasty.com/knowledge-base/magento-2-4-release-undefined-class-constant-error.html

@alexey-motorny-amasty: out of interest (since I saw your little amasty/module-mage24fix module come in with composer when updating the improved layered navigation module on a 2.3.5 shop). This little module is tested and is backwards compatible with Magento 2.3.x I hope?

@hostep Yes, it is compatible, but unnecessary. do not hesitate to just delete this patch

@Hexmage we have created patch - mini module to resolve this error. Maybe it will be useful for you
https://amasty.com/knowledge-base/magento-2-4-release-undefined-class-constant-error.html

Hi Hexmage, I have a problem with your this fix: #48 {main} [] []
[2020-08-14 13:30:16] main.CRITICAL: Error: Class 'Amasty\Mage24Fix\Block\Theme\Html\Title' not found in /public_html/generated/code/Amasty/Mage24Fix/Block/Theme/Html/Title/Interceptor.php:7

@mdehaan not my fix contact @alexey-motorny-amasty instead.

@mdehaan please check if you have copied all the files. or run setup:di:compile

@mdehaan please check if you have copied all the files. or run setup:di:compile
@alexey-motorny-amasty

I just fixed it.
I renamed module-mage24fix-master to Mage24Fix and now it works.

Hi @m2-assistant[bot]. Thank you for your report.
The issue has been fixed in magento/magento2#29925 by @toxix in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.2 release.

Was this page helpful?
0 / 5 - 0 ratings