Can't use "const" and "define" in PHP class - php

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?

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

Related

Get Value from object in php with 0 and 1 index [duplicate]

I'm working on an existing code base and got back an object with an attribute that starts with a number, which I can see if I call print_r on the object.
Let's say it's $Beeblebrox->2ndhead. When I try to access it like that, I get an error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
How can I get that attribute?
What about this :
$Beeblebrox->{'2ndhead'}
Actually, you can do this for pretty much any kind of variable -- even for ones that are not class properties.
For example, you could think about a variable's name that contains spaces ; the following syntax will work :
${"My test var"} = 10;
echo ${"My test var"};
Even if, obviously, you would not be able to do anything like this :
$My test var = 10;
echo $My test var;
No idea how it's working internally, though... And after a bit of searching, I cannot find anything about this in the PHP manual.
Only thing I can find about {} and variables is in here : Variable parsing -- but not quite related to the current subject...
But here's an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness
And here's another one that gives some additionnal informations about the way those are parsed : PHP grammar notes
I actually found out the answer from a coworker before I asked this, but couldn't find it on Google, so I wanted to post it here in case others have the same problem.
I can access that attribute like so:
$Beeblebrox->{'2ndhead'}
It's not really legal to have an attribute or variable that begins with a number, but somehow a dynamic reference like this makes it possible. Seems like a weird loophole in the language to me.
You can do something like this too:
$aux = '2ndhead';
$Beeblebrox->$aux;

PHP - Dynamic Class Name Inside Class Properties

I was wondering if it's possible to dynamically access a static class from within a class property. I'm trying to access the properties in a nchild class, but I'm having to do something like this:
$error = $this->errorClass;
$error::myMethod ();
What I really want to do is this:
$this->errorClass::myMethod ();
I keep getting PHP Parse error: syntax error, unexpected '::' If this isn't possible, does anybody know of a somewhat similar way to do things that doesn't involve repeating myself for every class/method? Thanks for your time!
$this->errorClass::myMethod();
Please explain to the parser what you want? $temp = errorClass::myMethod(); $this->$temp; or $temp = $this->errorClass; $temp::myMethod();.
Because of such problems this is impossible.
The shortest you can use (1 single statement...) is:
${'_'.!$this->errorClass}::myMethod();

How can I access an object attribute that starts with a number?

I'm working on an existing code base and got back an object with an attribute that starts with a number, which I can see if I call print_r on the object.
Let's say it's $Beeblebrox->2ndhead. When I try to access it like that, I get an error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
How can I get that attribute?
What about this :
$Beeblebrox->{'2ndhead'}
Actually, you can do this for pretty much any kind of variable -- even for ones that are not class properties.
For example, you could think about a variable's name that contains spaces ; the following syntax will work :
${"My test var"} = 10;
echo ${"My test var"};
Even if, obviously, you would not be able to do anything like this :
$My test var = 10;
echo $My test var;
No idea how it's working internally, though... And after a bit of searching, I cannot find anything about this in the PHP manual.
Only thing I can find about {} and variables is in here : Variable parsing -- but not quite related to the current subject...
But here's an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness
And here's another one that gives some additionnal informations about the way those are parsed : PHP grammar notes
I actually found out the answer from a coworker before I asked this, but couldn't find it on Google, so I wanted to post it here in case others have the same problem.
I can access that attribute like so:
$Beeblebrox->{'2ndhead'}
It's not really legal to have an attribute or variable that begins with a number, but somehow a dynamic reference like this makes it possible. Seems like a weird loophole in the language to me.
You can do something like this too:
$aux = '2ndhead';
$Beeblebrox->$aux;

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

I get this error in my PHP code:
PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\Inetpub\wwwroot\webroot\www.novotempo.org.br\lib\Twitter.php on line 54
The line in question:
define('DEBUG',false);
Searching the net I found that this usually occurs when you´re using PHP 4.xx, but I´m using 5.2.6 (Just checked it using phpinfo()).
I tried locally, and in two other external hosts, but it keeps returning the same message.
Why does this happen? How can I fix it?
If you are trying to DEFINE something inside of a class but outside of a function, you are going to get this error.
(Normally the only place PHP will be looking for a function and not expecting a string is in a class, outside of a method)
IE: Your code should not look like this:
class myClass
{
define("DEBUG", true);
function myFunc()
{
}
}
Thanks for this, resolved an annoying issue quite quickly for me.
I'd also like to add that the use of hyphens in constant names doesn't work and is apparently expected behaviour. It seems as though the echo tries to evaluate mathematics (minus) on the words.
<?php
define('THIS-IS-A-TEST','Testing');
echo THIS-IS-A-TEST;
?>
Returns '0'
<?php
define('THIS_IS_A_TEST','Testing');
echo THIS_IS_A_TEST;
?>
Returns 'Testing'

Why is specifying a defined constant inside a class constant throwing an error?

Here is part of my code
define('DIR_APP', 'app/');
class Questions
{
const QUESTIONS_FILE = DIR_APP . 'questions.xml';
}
Seems when I take the define()'d constant out of my class constant declaration, it works fine. But when I add it it throws this error:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in /home/public_html/app/classes/Questions.class.php on line 7
So how can I get my define()'d constant in there? I assume it is not correctly looking up the DIR_APP thinking it might of been defined within the class. How do I get it to resolve it globally?
Thank you
It can't be done.
Quote from http://www.php.net/manual/en/language.oop5.constants.php :
"The value must be a constant
expression, not (for example) a
variable, a class member, result of a
mathematical operation or a function
call"
I think you could
define('QUESTIONS_FILE', DIR_APP .
'questions.xml');
but that is global.
Never tried that before. Zend Studio is giving me an issue at the moment to look at it myself. How about trying this though...
const QUESTIONS_FILE = constant("DIR_APP") . 'questions.xml';

Categories