Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

constants

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?

like image 346
phoops Avatar asked Apr 02 '14 09:04

phoops


People also ask

What is fix-PHP notice use of undefined constant?

Fix - PHP Notice: Use of undefined constant. Fix – PHP Notice: Use of undefined constant. This is a common notice / warning that occurs whenever PHP has detected the usage of an undefined constant. In case you didn’t already know, a constant is a simple value that cannot change during the execution of a script. i.e.

What is an undefined constant error in PHP?

This is a common notice / warning that occurs whenever PHP has detected the usage of an undefined constant. In case you didn’t already know, a constant is a simple value that cannot change during the execution of a script. i.e. If you define constant A as “123”, you won’t be able to change it to “456” at a later stage.

What happens if you don't define constants in PHP?

When it doesn't find such a constant, PHP (bizarrely) interprets it as a string ('department', etc). Obviously, this can easily break if you do defined such a constant later (though it's bad style to have lower-case constants).

Why am I seeing'use of undefined request_URI'in my Apache server logs?

If you are seeing the Warning for ' Use of undefined REQUEST_URI ' in your Apache server logs, don't worry. This can easily be fixed. Before jumping to the solution let's understand why PHP gives this warning. This is more of a syntax warning, which PHP is allowing for now as PHP is an easy-going language when it comes to syntax.


1 Answers

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;
like image 84
phoops Avatar answered Oct 22 '22 04:10

phoops