I am pulling my hair out on this one. Pretty new at PHP but this is so basic I just can't figure out where the problem is. Using the code snippet below as an example:
class LG_Activity_Processor {
// Activity Types
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISH = 'publish';
...
private $STATUS_FUTURE = 'future';
define ("STATUS_PRIVATE" , 'private');
I had originally intended to just use the "const" construct as the variables are fully defined prior to run time and I just think the syntax is prettier than that ugly "define". The problem is while the definition of the const gives no errors, whenever I refer to the constant later in the class I get the following error message:
PHP Notice: Use of undefined constant STATUS_PUBLISH - assumed 'STATUS_PUBLISH'
Huh? Just to be clear, here's the syntax I used to reference the "const":
$core_fields ['post_status'] = STATUS_PUBLISH;
I even tried:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
No love. I then entered a state of despair and eventually tried "define". Same calling syntax but I changed the definition syntax to that as illustrated above for "STATUS_PRIVATE". This made things far worth as I now had a fatal error on the define line that 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 example of STATUS_FUTURE) and then referred to it as:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
That works just like you'd expect it to but I can't help but feeling I've been cheated out of doing it the right way. Any ideas on how to make my code whole again?
constis what you need. UseClassName::CONST_NAMEoutside of a class, and in the same class, just useself::CONST_NAME.