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?

6
  • 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. Commented Apr 2, 2014 at 9: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. Commented Apr 2, 2014 at 9:36
  • Notice that it says use of undefined constant, not class constant... Commented Apr 2, 2014 at 9:41
  • Yes, but I do not think PHP Notices really distinguish between class and non-class constant. Commented Apr 2, 2014 at 9:41
  • They do: 3v4l.org/8bFNJ Commented Apr 2, 2014 at 9:44

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

PHP should certainly have pointed you to that line. It's very weird it didn't.
Yes that is what I would also expect. I have filed a BUG report bugs.php.net/bug.php?id=67007 to see where it gets me.
0

See the difference PHP | 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.

5 Comments

Ok this seems helpful. But how do I define these constants for in a specific class then?
@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()
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
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.