3

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?

1
  • Don't use define() in classes - const is what you need. Use ClassName::CONST_NAME outside of a class, and in the same class, just use self::CONST_NAME. Commented Apr 30, 2012 at 23:02

1 Answer 1

6

You should do LG_Activity_Processor::STATUS_DRAFT when accessing it.

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Or just self::STATUS_DRAFT if used within the scope of the class itself.
@Madmartigan that is only available if you are inside the class. It will work here, but using the full classname will work everywhere.
I assumed he was working in the scope of the class by seeing the code $this->STATUS_PUBLISH
I am working within the scope of the class itself. I'll try the self:: abbreviation as soon as I've dropped the dog off this morning. I could have sworn I had tried the fully qualified LG_Acitivity_Processor:: but maybe I didn't. In any regard, thanks for the pointer and I'll hopefully close this out in the next hour or so.

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.