php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39466 Expaned Class constants to accept return of function as value
Submitted: 2006-11-10 22:11 UTC Modified: 2007-05-18 12:00 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: kevin at metalaxe dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.0+ OS: *
Private report: No CVE-ID: None
 [2006-11-10 22:11 UTC] kevin at metalaxe dot com
Description:
------------
You can define a global constant based on the return of a function yet you cannot do so for class constants.
eg. define( 'MAGIC_QUOTES', ( (bool)get_magic_quotes_gpc() ), true );

Requesting a method to assign class constants in a manner similar to global constants

Reproduce code:
---------------
<?php
class parser
{
    const magic_quotes = (bool)get_magic_quotes_gpc();

    public my_stripslashes( $str )
    {
         if( self::magic_quotes === true )
         {
             $str = stripslashes( $str );
         }
         return $str;
    }
}
?>

Expected result:
----------------
Constant "magic_quotes" to be assigned the boolean value of get_magic_quotes_gpc's return.

Actual result:
--------------
With boolean cast: 
Parse error: syntax error, unexpected T_BOOL_CAST in parser.php on line 4
Without boolean cast:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in parser.php on line 4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-10 22:26 UTC] tony2001@php.net
That's not possible, as constants are declared at compile time, while functions are executed at runtime.
 [2006-11-10 22:33 UTC] kevin at metalaxe dot com
Ok, if that is the case, how does the global constant MAGIC_QUOTES get defined from the code in the description?

<?php
define( 'MAGIC_QUOTES', ( (bool)get_magic_quotes_gpc() ), true );
var_dump( MAGIC_QUOTES );
?>

prints bool(true)

We are running 3 functions there (according to the docs). One to get the value being applied, another to cast to int and finally one to "define" the constant.
 [2006-11-10 22:37 UTC] kevin at metalaxe dot com
Just for reference, I reread the docs for global constants set using define(). http://us2.php.net/manual/en/function.define.php states:

Defines a named constant at runtime. See the section on constants for more details.
 [2006-11-10 22:40 UTC] tony2001@php.net
define() is just a function. which declares constants.
I was talking about class constants et al.
 [2006-11-10 22:54 UTC] kevin at metalaxe dot com
I don't understand why I'm being shrugged off as impossible. I'm asking for functionality similar to global constants for class constants. If you can do it for global constants, it seems only plausible that class constants should be handled in a similar fashion.
I could find many more uses for the exact same functionality if necessary, within the same applicable class even.
 [2006-11-10 23:17 UTC] kevin at metalaxe dot com
I've thought on this a bit more. It seems that php currently has the idea of constants set-up a bit limited in sight. While PHP has "constants"implimented correctly in the class scope, it doesn't implement it in this way on the global scope. It is silly that functionality of such a feature should change in such a drastic manner.

If you are unwilling to change the functionality of class keyword const, then you need to implement a method as to set a variable read-only within a class. Which is just completely redundant in nature and contrary to the cause.

Perhaps you could rethink how you are implementing the const keyword and revamp it to assign a member as read-only. This might even help you with implementing the visibility scopes (public, private, protected) to a class constant. Because again, it is perfectly plausible to have a constant accessible only within the scope of a single class or child classes.
 [2006-11-10 23:23 UTC] tony2001@php.net
Again, class constants along with the classes and functions are declared in compile time, so they can have only constant value.
Common constants are declared in runtime using define() function, which is why it may take a result of an expression.
There is nothing to be done with that unless you're willing to rewrite all the engine from scratch.

 [2007-05-18 08:18 UTC] kevin at metalaxe dot com
I still don't know how a suggestion can be marked bogus ;)

There is no reason to close it as the request is perfectly acceptable as a suggestion or feature request. If you don't want to "re-write the engine" to maintain consistency, then perhaps someone else will one day :)
 [2007-05-18 08:32 UTC] kevin at metalaxe dot com
"Common constants are declared in runtime using define() function, which
is why it may take a result of an expression."

Why not expand define() to allow setting a class specific constant that can be called as $this->class_name->some_constant ? As it stands now I have to write excess functions that will return the values of specified class variables. Adding the ability to add constants within the spoce of a single class would remove that need :)
 [2007-05-18 12:00 UTC] tony2001@php.net
>I still don't know how a suggestion can be marked bogus

I can show you.

>There is no reason to close it as the request is perfectly
>acceptable as a suggestion or feature request. 

A feature request for renaming PHP to XXX is perfectly valid also, but there is no point to keep it open.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 16:01:31 2024 UTC