SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __unset array-element

    hi,

    PHP Code:
    <?php
       
    class Session {
          public function 
    __construct() {
             
    session_start();
             
    session_regenerate_id(true);
          }
          public function 
    __destruct() {
             unset(
    $_SESSION['sess']);
          }
          public function 
    __set($key,$value) {
             
    $_SESSION['sess'][$key]=$value;
          }
          public function 
    __get($key) {
             return 
    array_key_exists($key,$_SESSION['sess'])
             ?  
    $_SESSION['sess'][$key]
             :  
    null;
          }
          public function 
    __isset($key) {
             return isset(
    $_SESSION['sess'][$key]);
          }
          public function 
    __unset($key) {
             echo 
    '__unset<br />';
             unset(
    $_SESSION['sess'][$key]);
          }
       }
       
    $s=new Session();
       
    $_SESSION['check']='c';
       
    $s->k1=array (
          
    'a'=>2,
          
    'b'=>3
       
    );
       echo 
    '<b>k1["a"]:</b><br />';
       
    var_dump($s->k1['a']);
       echo 
    '<br /><b>isset k1["a"]:</b><br />';
       
    var_dump(isset($s->k1['a']));
       echo 
    '<br /><b>isset k1["x"]:</b><br />';
       
    var_dump(isset($s->k1['x']));
       echo 
    '<br /><b>SESSION:</b><br />';
       
    var_dump($_SESSION);
       echo 
    '<br /><b>unset k1["a"]:</b><br />';
       unset(
    $s->k1['a']); // DOESN´T WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    var_dump($_SESSION);
       echo 
    '<br /><b>unset k1:</b><br />';
       unset(
    $s->k1);
       
    var_dump($_SESSION);
    ?>
    unset($s->k1['a']); doesn&#180;t work. Method __unset isn&#180;t called. Is it a PHP-bug?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    It's not a bug, trying to modify an overloaded property (k1) indirectly is not allowed as the notice which is raised when running your says.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I forgot an error_reporting(E_ALL|E_STRICT);

    "Indirect modification of overloaded property"
    What does this mean?
    What&#180;s an "indirect" modification? Modification with the unsetter-Method? (the setter-Method seems to work)
    And what means "overloaded"? Arrays (and Objects) are not allowed?

    EDIT: I think, I have understood the problem. You can save arrays, but you can&#180;t add or delete array-elements.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •