Possible PHP Bug With const?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Possible PHP Bug With const?

Post by jackpf »

Hi,

I don't know if this is supposed to happen or not...but check this out:

Code: Select all

<?php
define('one', 1); // one will be 1
const two = 2; // two will be 2 (as of php 5.3)
 
define('one_or_two', 1 | 2); // one_or_two will be 3
const two_or_three = 2 | 3; // error
?>
This is quite annoying, because I need to use the or operator in one of my class constants. I could just add the values up...but this makes more sense - next time I go back to look over my code, instead of seeing a random number and wondering what it's for, I'll see the comparison.

So yeah, has anyone ever come across something similar, or even better, know a way to fix it? :?

Thanks, Jack.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Possible PHP Bug With const?

Post by AbraCadaver »

What do you want to happen? Performing a bitwise OR (1 | 2) is 3. If you need to use the expression 1 | 2 then it needs to be a string:

define('one_or_two', '1 | 2');

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Possible PHP Bug With const?

Post by jackpf »

I think you misunderstand.

define() works fine. It's using "const" that doesn't.

If you test out my script, you'll see what I mean.

I don't see how using a string will work :/

Cheers,
Jack.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Possible PHP Bug With const?

Post by AbraCadaver »

For const http://us.php.net/manual/en/language.oop5.constants.php: The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

This is not the case for define().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Possible PHP Bug With const?

Post by jackpf »

Oh right.

That's stupid :/

Oh well. Thanks for pointing that out AbraCadaver.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Possible PHP Bug With const?

Post by AbraCadaver »

Yeah, it is inconvenient that they would treat them differently, but classes, class constants and functions are declared compile time, so they can have only constant values. Global constants declared with define() are declared runtime so they can take the result of an expression.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply