php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39467 Expaned Class members to allow setting of a read only value at runtime
Submitted: 2006-11-10 23:47 UTC Modified: 2012-03-14 01:36 UTC
Votes:8
Avg. Score:4.8 ± 0.4
Reproduced:7 of 8 (87.5%)
Same Version:5 (71.4%)
Same OS:6 (85.7%)
From: kevin at metalaxe dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.0 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-11-10 23:47 UTC] kevin at metalaxe dot com
Description:
------------
Since http://bugs.php.net/bug.php?id=39466 was shot down, PHP is in need of a method to which we can set class member variables as read-only during runtime to prevent changing of values that are critically important for script execution.

I would like to use a class constant for this but, because it cannot be assigned a value at runtime, it is impossible to do so. The reproduce code is an example of a value that I would like to set as read only. The scope of the variable should not matter in this suggestion as it is practical that it would need to be changed given a proper reason.

Reproduce code:
---------------
<?php
class parser
{
    public $magic_quotes = false;

    public __construct()
    {
        //Add some sort of identifier here to set the
        //value read only
        (readonly)$this->magic_quotes = (bool)get_magic_quotes_gpc();
    }

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

    public change_magic_quotes($to)
    {
         $this->magic_quotes = $to;
    }
}

$parser = new parser();

//Returns stripped string
$stripped = $parser->my_stripslashes( 'Hi, there\'s a coke in the fridge' );

//Doesn't change the value of magic_quotes and flags an
//E_WARNING or E_NOTICE error.
$parser->change_magic_quotes( true );
?>

Expected result:
----------------
Hope to have it not allow the variable to be changed (ha, a constant, what a silly notion) and pop an error of some kind to the parser.

Actual result:
--------------
Not implemented, thus values can be changed at any time as long as the variable is within the visibility scope of calling party.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-10 23:49 UTC] kevin at metalaxe dot com
There is a typo in my example code above.

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

should be

    public my_stripslashes( $str )
    {
         if( $this->magic_quotes === true )
         {
             $str = stripslashes( $str );
         }
         return $str;
    }
 [2012-03-14 01:29 UTC] uramihsayibok at gmail dot com
If you need to define the "constant" in your constructor then what you have isn't a constant. So if you don't 
want it public then don't make it public.

  class parser {
    private $_magic_quotes = false;
    function __construct() { $this->_magic_quotes = /* etc */; }
  }

Otherwise you have to define the constant outside the class and if that's the case then you might as well use 
define(). If you're really hung up on the class constant syntax then you can

  define("MAGIC_QUOTES", /* etc  */);
  class parser {
    const MAGIC_QUOTES = MAGIC_QUOTES;
  }

Side notes:
1. Though this isn't the place for discussion, if it were I'd talk about how a global constant for this 
particular example makes more sense anyways.
2. Since magic_quotes *and its functions* are going way the best way to check for it is
  (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc())
 [2012-03-14 01:36 UTC] aharvey@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2012-03-14 01:36 UTC] aharvey@php.net
A private field plus an accessor with the desired visibility would work fine here 
without needing a language extension.

Closing won't fix.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC