Reason for Undefined class constant NOTICE in PHP (Use of undefined constant)

10,626

I have found the reason for this notice and fixed it.

I have had this line in Result class:

protected $code = PAYMENT_ERROR;

This was causing the notice above, as I did not define this correctly. I would have expected PHP to tell me where the error message was coming from exactly, when instantiating new Class, instead of just pointing to a line where said Class is instaniated.

So the fix was to change it to this:

protected $code = self::PAYMENT_ERROR;
Share:
10,626

Related videos on Youtube

phoops
Author by

phoops

Updated on June 04, 2022

Comments

  • phoops
    phoops almost 2 years

    I have discovered a weird problem in my code regarding class constants. While it seems that the code does work correctly, I cannot figure out the reason of PHP Notice I am getting:

    Use of undefined constant PAYMENT_ERROR - assumed 'PAYMENT_ERROR' in /src/Micro/Payments/Manager.php on line 146

    The code in Manager.php function looks like this:

    $code = Result::PAYMENT_ERROR;
    return new Result($code, $errMsg); // <- line 146 - causes PHP Notice
    

    What is strange to me, is that $code variable is set correctly and does not trigger any notices. Only instantiating Result does.

    The Result class is very simple:

    class Result
    {
        // ... boilerplate code skipped ...
        // constant is defined like this:
        const PAYMENT_ERROR = 2;
    
        public function __construct($code, array $messages)
        {
            $this->code = $code;
            $this->messages = $messages;
        }
    
        // ... other functions skipped as they are not relevat ...
    }
    

    Is there a problem that I pass Result's constant to it's own constructor?

    • Gromski
      Gromski about 10 years
      If anything that notice must be triggered on the previous line where you write Result::PAYMENT_ERROR. It is not possible that the use of $code triggers this notice. Which makes it likely you're looking at the wrong file or have other issues identifying the correct piece of source code.
  • phoops
    phoops about 10 years
    Ok this seems helpful. But how do I define these constants for in a specific class then?
  • sandip
    sandip about 10 years
    @edvinas.me and where do you want to use that contant? inside class? then this error should not come, but you want to use this out side the class you can go ahead with define()
  • phoops
    phoops about 10 years
    No, this does not really help. I want these constants defined in Result, and then create new Result, by passing Result's constants to one of the constructors. The same way as described in php.net/manual/en/language.oop5.constants.php
  • phoops
    phoops about 10 years
    And my code does work fine, it's just that the reason for the Notice is Unknown. I start to thin that it might be related to something else in Result
  • phoops
    phoops about 10 years
    I have found the issue and fixed this. The error was coming from other line in Result class which incorrectly assigned initial value of $code to default to PAYMENT_ERROR constant. You can find more info in the answer I provided.
  • Gromski
    Gromski about 10 years
    PHP should certainly have pointed you to that line. It's very weird it didn't.