Skip to content

[Issue] Fixed 'Undefined class constant' error when interceptor is generated. #28981

Closed
@m2-assistant

Description

@m2-assistant

This issue is automatically created based on existing pull request: #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: ThemeFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentPriority: P4No current plan to fix. Fixing can be deferred as a logical part of more important work.Progress: doneReported on 2.4.0Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S3Affects non-critical data or functionality and does not force users to employ a workaround.Triage: DoneHas been reviewed and prioritized during Triage with Product Managers

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions