ArrayAccess Interface et tableaux multi-dimensionnels

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Juillet 9th, 2008, 3:03 am

Im un peu de mal mise en oeuvre du PHP ArrayAccess interface lorsque les tableaux multi-dimensionnels sont impliqués.

Je peux obtenir ajoute à travailler

Code: [ Select ]
$myobject[] = 'new value';


Je peux obtenir des assignations de haut niveau pour travailler

Code: [ Select ]
$myobject['index'] = 'New Value';


Je peux accéder aux valeurs imbriquées si elle est définie comme tant theyre

Code: [ Select ]
$myobject['index'] = array('zero');
echo $myobject['index'][0];
  1. $myobject['index'] = array('zero');
  2. echo $myobject['index'][0];


Cependant, si j'essaie d'affectation à un indice de imbriqués, il semble avoir complètement ignoré si l'indice de haut niveau existe ou non.

Code: [ Select ]
$myobject['index']['one'] = 1;


Im commence à me demander si l'interface ArrayAccess est censé être exécuté directement depuis le tout en regardant Ive Ive a découvert des classes comme ArrayObject implémenter l'interface ArrayAccess.

Heres la classe avec mon débogage gauche, peut-on voir quelque chose cloche dans la façon Ive a tenté d'appliquer ce que c'est?

Code: [ Select ]
function echo_r($data)
{
    echo sprintf('<pre>%s</pre>', print_r($data, true));
}
 
abstract class identifiable
{
    public $id, $name;
}
 
final class language extends identifiable implements ArrayAccess
{
    public $texts = array();
   
    public function load($ini_file)
    {
        //$this->texts = array_merge_recursive($this->texts, parse_ini_file($ini_file));
        return $this;
    }
 
    public function __construct($id, $name = 'New Language', $ini_file = null)
    {
        return $ini_file ? $this->load($ini_file) : $this;
    }
   
    public function offsetExists($offset)
    {echo "[e:$offset]";
        return isset($this->texts[$offset]);
    }
   
    public function offsetGet($offset)
    {echo "[g:$offset]";
        return $this->texts[$offset];
    }
   
    public function offsetSet($offset, $value)
    {echo "[s:$offset--$value]";
        if(empty($offset))
        {
            $this->texts[] = $value;
            return;
        }
        $this->texts[$offset] = $value;
    }
   
    public function offsetUnset($offset)
    {echo "[u:$offset]";
        unset($this->texts[$offset]);
    }
}
 
 
$lang = new language('test', 'Test Language', 'languages/test.ini');
$lang[] = 'appended';
//$lang['stuff'] = array();
$lang['stuff'][] = 'multi-stuff';
echo_r($lang);
 
///////////////////////////////////////////
[s:--appended][g:stuff]
language Object
(
    [texts] => Array
        (
            [0] => appended
        )
 
    [id] =>
    [name] =>
)
  1. function echo_r($data)
  2. {
  3.     echo sprintf('<pre>%s</pre>', print_r($data, true));
  4. }
  5.  
  6. abstract class identifiable
  7. {
  8.     public $id, $name;
  9. }
  10.  
  11. final class language extends identifiable implements ArrayAccess
  12. {
  13.     public $texts = array();
  14.    
  15.     public function load($ini_file)
  16.     {
  17.         //$this->texts = array_merge_recursive($this->texts, parse_ini_file($ini_file));
  18.         return $this;
  19.     }
  20.  
  21.     public function __construct($id, $name = 'New Language', $ini_file = null)
  22.     {
  23.         return $ini_file ? $this->load($ini_file) : $this;
  24.     }
  25.    
  26.     public function offsetExists($offset)
  27.     {echo "[e:$offset]";
  28.         return isset($this->texts[$offset]);
  29.     }
  30.    
  31.     public function offsetGet($offset)
  32.     {echo "[g:$offset]";
  33.         return $this->texts[$offset];
  34.     }
  35.    
  36.     public function offsetSet($offset, $value)
  37.     {echo "[s:$offset--$value]";
  38.         if(empty($offset))
  39.         {
  40.             $this->texts[] = $value;
  41.             return;
  42.         }
  43.         $this->texts[$offset] = $value;
  44.     }
  45.    
  46.     public function offsetUnset($offset)
  47.     {echo "[u:$offset]";
  48.         unset($this->texts[$offset]);
  49.     }
  50. }
  51.  
  52.  
  53. $lang = new language('test', 'Test Language', 'languages/test.ini');
  54. $lang[] = 'appended';
  55. //$lang['stuff'] = array();
  56. $lang['stuff'][] = 'multi-stuff';
  57. echo_r($lang);
  58.  
  59. ///////////////////////////////////////////
  60. [s:--appended][g:stuff]
  61. language Object
  62. (
  63.     [texts] => Array
  64.         (
  65.             [0] => appended
  66.         )
  67.  
  68.     [id] =>
  69.     [name] =>
  70. )
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Juillet 9th, 2008, 3:03 am

  • spork
  • Brewmaster
  • Silver Member
  • Avatar de l’utilisateur
  • Inscription: Sep 22, 2003
  • Messages: 6130
  • Loc: Seattle, WA
  • Status: Offline

Message Juillet 9th, 2008, 7:32 am

PHP.net a écrit:
ArrayObject / ArrayIterator simplement appliquer les ArrayAccess interface qui ne gère pas les multiples dimensions de syntaxe . Comme vous l'avez souligné vous avez déjà pu ArrayObject magasin ArrayObjects lui-même. Ainsi, la solution est d'écraser ArrayObject.

http://bugs.php.net/bug.php?id=34816

Vous êtes peut-être pas de chance sur celui-ci :|
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Juillet 9th, 2008, 12:02 pm

Bummer.
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de l’utilisateur
  • Inscription: Juil 14, 2005
  • Messages: 8211
  • Loc: USA
  • Status: Offline

Message Juillet 9th, 2008, 12:51 pm

Est cet d'aucune aide?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Juillet 9th, 2008, 2:22 pm

Im va se pencher sur la ArrayObject de ArrayObjects. Même si j'ai besoin d'étendre de manière identifiable "instanceof identifiables" fonctionne.
Strong with this one, the sudo is.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Inscription: Fév 10, 2004
  • Messages: 13455
  • Loc: Florida
  • Status: Offline

Message Juillet 9th, 2008, 11:27 pm

A décidé d'abandonner la pensée originale & go avec le code suivant au lieu depuis que j'ai eu besoin d'un analyseur de toute façon souple ini.

Code: [ Select ]
abstract class identifiable
{
    public $id, $name;
}

final class language extends identifiable
{
    public $texts = array();
    
    public function load($ini_file)
    {
        $newlines = file($ini_file);
        if($newlines !== false)
        {
            $current_category = '$this->';
            foreach($newlines as &$line)
            {
                if((bool)preg_match('#^([a-z][a-z\d_.]*)\s*=(.+)$#i', $line, $entry))
                {
                    $entry[2] = trim($entry[2], '\'"');
                    eval(sprintf('%s%s="%s";',
                        $current_category,
                        str_replace('.', '->', strtolower($entry[1])),
                        preg_replace('#\{2,}"#', '\"', str_replace('"', '\"', $entry[2]))
                    ));
                }
                else if((bool)preg_match('#^\s*\[([a-z][a-z\d_.]*)\]\s*$#i', $line, $category))
                {
                    $current_category = sprintf('$this->%s->', str_replace('.', '->', strtolower($category[1])));
                }
                unset($line);
            }
        }
        return $this;
    }

    public function __construct($id, $name = 'New Language', $ini_file = null)
    {
        $this->id = $id;
        $this->name = $name;
        return $ini_file ? $this->load($ini_file) : $this;
    }

    public function __set($key, $val)
    {
        $this->texts[strtolower($key)] = $val;
    }
    
    public function &__get($key)
    {
        return $this->texts[strtolower($key)];
    }
}
  1. abstract class identifiable
  2. {
  3.     public $id, $name;
  4. }
  5. final class language extends identifiable
  6. {
  7.     public $texts = array();
  8.     
  9.     public function load($ini_file)
  10.     {
  11.         $newlines = file($ini_file);
  12.         if($newlines !== false)
  13.         {
  14.             $current_category = '$this->';
  15.             foreach($newlines as &$line)
  16.             {
  17.                 if((bool)preg_match('#^([a-z][a-z\d_.]*)\s*=(.+)$#i', $line, $entry))
  18.                 {
  19.                     $entry[2] = trim($entry[2], '\'"');
  20.                     eval(sprintf('%s%s="%s";',
  21.                         $current_category,
  22.                         str_replace('.', '->', strtolower($entry[1])),
  23.                         preg_replace('#\{2,}"#', '\"', str_replace('"', '\"', $entry[2]))
  24.                     ));
  25.                 }
  26.                 else if((bool)preg_match('#^\s*\[([a-z][a-z\d_.]*)\]\s*$#i', $line, $category))
  27.                 {
  28.                     $current_category = sprintf('$this->%s->', str_replace('.', '->', strtolower($category[1])));
  29.                 }
  30.                 unset($line);
  31.             }
  32.         }
  33.         return $this;
  34.     }
  35.     public function __construct($id, $name = 'New Language', $ini_file = null)
  36.     {
  37.         $this->id = $id;
  38.         $this->name = $name;
  39.         return $ini_file ? $this->load($ini_file) : $this;
  40.     }
  41.     public function __set($key, $val)
  42.     {
  43.         $this->texts[strtolower($key)] = $val;
  44.     }
  45.     
  46.     public function &__get($key)
  47.     {
  48.         return $this->texts[strtolower($key)];
  49.     }
  50. }
Strong with this one, the sudo is.
  • zniko07
  • Born
  • Born
  • No Avatar
  • Inscription: Mar 20, 2010
  • Messages: 1
  • Status: Offline

Message Mars 20th, 2010, 3:59 am

salut!
Je ne suis pas sûr de celui-ci mais si vous implémentez l'offsetGet
comme celui-ci
fonction offsetGet ($ offset) (
echo request offset:. $ offset. <br />;
return isset ($ this-> vars [$ offset])? $ this-> vars [$ offset]: self nouvelles;
)

vous mai obtenir ce que tu voulais.

Afficher de l'information

  • Total des messages de ce sujet: 7 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 238 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC