7

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?

ek9
  • 3,392
  • 5
  • 23
  • 34
  • 1
    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. – deceze Apr 02 '14 at 09:32
  • @dezece: I have thought same way as you, but I have tested it like this: I have added `die(var_dump($code));` after assigning `$code` and the `$code` outputs correct value for $code (taken from Result::PAYMENT_ERROR constant), and does NOT show notice. So the error is indeed caused by `return new Result($code, $errMsg);`. Thanks for feedback though. This is also the reason why I added `$code` in general, as I was previously instantiating result by passing constant directly, not via `$code`. But that also throw's same Notice, thus I posted here. – ek9 Apr 02 '14 at 09:36
  • Notice that it says *use of undefined constant*, not *class constant*... – deceze Apr 02 '14 at 09:41
  • Yes, but I do not think PHP Notices really distinguish between class and non-class constant. – ek9 Apr 02 '14 at 09:41
  • They do: http://3v4l.org/8bFNJ – deceze Apr 02 '14 at 09:44
  • Thanks, your tips lead me to find the real cause of this. – ek9 Apr 02 '14 at 09:56

2 Answers2

7

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;
ek9
  • 3,392
  • 5
  • 23
  • 34
  • 1
    PHP *should* certainly have pointed you to that line. It's very weird it didn't. – deceze Apr 02 '14 at 10:00
  • Yes that is what I would also expect. I have filed a BUG report https://bugs.php.net/bug.php?id=67007 to see where it gets me. – ek9 Apr 02 '14 at 11:39
0

See the difference define() vs const

You must be using the PAYMENT_ERROR outside the class.

If you want to do so use the define().

This will do the job.

Community
  • 1
  • 1
sandip
  • 3,279
  • 5
  • 31
  • 54
  • Ok this seems helpful. But how do I define these constants for in a specific class then? – ek9 Apr 02 '14 at 09:42
  • @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() – sandip Apr 02 '14 at 09:45
  • 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 http://www.php.net/manual/en/language.oop5.constants.php – ek9 Apr 02 '14 at 09:49
  • 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 – ek9 Apr 02 '14 at 09:49
  • 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. – ek9 Apr 02 '14 at 09:58