Cannot use "const" and "define" in PHP class

I pull my hair out on this one. Pretty new to PHP, but it's that simple, I just can't figure out where the problem is. Using the code snippet below:

class LG_Activity_Processor {
// Activity Types
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISH = 'publish';
...
private $STATUS_FUTURE = 'future';
define ("STATUS_PRIVATE" , 'private');

I originally planned to use the "const" construct because the variables are fully defined before runtime, and I just think the syntax is prettier than this ugly "definition". The problem is that the definition of const gives no errors, whenever I refer to a constant later in the class, I get the following error message:

PHP note: using undefined constants STATUS_PUBLISH - assumed 'STATUS_PUBLISH'

AND? To be clear, here is the syntax I used to refer to "const":

$core_fields ['post_status'] = STATUS_PUBLISH;

I even tried:

$core_fields ['post_status'] = $this->STATUS_PUBLISH;

No love. Then I went into a state of despair and eventually tried to "determine." The same call syntax, but I changed the definition syntax as shown above for "STATUS_PRIVATE". This made things much more expensive, as I now had a fatal error in the definition line, which looked like this:

PHP Parse error:  syntax error, unexpected T_STRING, expecting T_FUNCTION

I gave up. I finally just defined the variable as a private variable (as in the STATUS_FUTURE example), and then calls it as:

$core_fields ['post_status'] = $this->STATUS_PUBLISH;

It works as you expected, but I cannot help but feel that I was deceived by doing it right. Any ideas on how to make my code whole again?

+3
source share
1 answer

When accessing it you have to do LG_Activity_Processor::STATUS_DRAFT.

Take a look at the PHP manual, it gives you clear examples.

http://php.net/manual/en/language.oop5.constants.php

+6
source

All Articles