Précédent   Forum des professionnels en informatique > PHP > Langage
Langage Forum sur le langage PHP, la POO, les conventions, la sécurité, etc. Avant de poster : FAQ Langage, toutes les FAQ PHP, cours langage et sources PHP
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 22/08/2011, 23h59   #1
Membre Expert
 
Avatar de gene69
 
Inscription : janvier 2006
Messages : 951
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : janvier 2006
Messages : 951
Points : 1 063
Points : 1 063
Par défaut probleme avec la classe ArrayObject

Bonjour

J'ai un gros soucis avec ArrayObject. j'ai developpé un outil qui analyse un texte et qui en fait des tableaux de tableaux etc...
j'ai voulu remplacé un constructeur array() par new ArrayObject() en me dissant que ça devrait tout être compatible... que nenni



j'ai une trace qui ressemble à ça:
il faut voir que l'arrayobject a des valeurs internes qui sont toutes les mêmes...
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => item
    [1] => plaspistol_ammo
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => name
    [1] => _Plasma Pistol Charger
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                    [0] => model
                    [1] => weapons/plaspistol/plaspistol_clip
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => model
    [1] => weapons/plaspistol/plaspistol_clip
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                )
 
            [1] => Array
                (
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                    [0] => type
                    [1] => ammo
                )
 
            [1] => Array
                (
                    [0] => type
                    [1] => ammo
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => type
    [1] => ammo
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                )
 
            [1] => Array
                (
                )
 
            [2] => Array
                (
                )
 
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Array
                (
                    [0] => animationindex
                    [1] => 0
                )
 
            [1] => Array
                (
                    [0] => animationindex
                    [1] => 0
                )
 
            [2] => Array
                (
                    [0] => animationindex
                    [1] => 0
                )
 
        )
 
)
et des bouts de codes

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 
<?php
Class UfoParser 
{
	protected $filename;
	protected $tokens;
 
	function __construct($filename)
	{
		if ( file_exists($filename) )
		{
			$this->filename = $filename;
			$this->tokens= token_get_all( "<?php ".file_get_contents($filename));
//			Debug::here();
		}
		else
		{
			Debug::var_dump($filename);
			throw new MyException("file error");
		}
	}
 
	/***/
	function analyze()
	{
		/**
		 * don't use class var here... need them recursivelys
		 **/
//		$symbol = array();//
		$symbol = new ArrayObject();
		$context =array();
		$index = 0;
//		$p =  new Prompt();
		$separator_before = true; // $previous_was_whitespaces
		while( $token = next($this->tokens) )
		{
			if ( is_array( $token ) )
			{
				if ( $token[0] == T_COMMENT )
				{
					$separator_before = true;
					// if we have a end-of-line comment (//), we have to "end" the line.
					// i think we just have to test this: $token[1][1] === '/'
					if( /* more easy to read, no equivalent... */ strpos($token[1],"\n") !== false )
					{
						self::meetTerminal($symbol, $context, $index );
					}
				}
				else if (  strpos($token[1],"\n") !== false  )
				{
					self::meetTerminal($symbol, $context, $index );
					$separator_before = true;
				}
				else if ( $token[0] === T_WHITESPACE )
				{
					$separator_before = true;
				}
				else
				{
					self::stackOrConcat($context,self::unquoteString($token[1]),$separator_before );
					$separator_before = false;
				}
			}
			else //token is a string (not an array in fact)
			{
				if ( $token === '{' )
				{
					$separator_before = true;
					if ( 0 == count($context) ) // case where there is an line end "before" the {
					{
						$symbol[$index-1][]= $this->analyze();
Debug::print_r($symbol);
					}
					else // there is
					{
						$context[] = $this->analyze();
Debug::print_r($context);
						self::meetTerminal($symbol,$context, $index);
					}
				}
				else if ( $token === '}' )
				{
					$separator_before = true;
					//fin de la bataille préparer tout pour retour.
					self::meetTerminal($symbol,$context, $index);
					return $symbol;
				}
				else if ( is_string($token) )
				{
					self::stackOrConcat($context,$token, $separator_before );
					$separator_before = false;
				}
				else
				{
					throw exception('This exception should  N E V E R  run');
				}
			}
		}//while
 
		self::meetTerminal($symbol,$context, $index);
		return $symbol;
 
	}//fct
 
 
	static protected function stackOrConcat( &$context, $token, $separator_before )
	{
		if ( $separator_before )
		{
			$context[]= $token;// push, don't concat.
		}
		else
		{
			$tmp = array_pop($context);
			$tmp.=$token;
			$context[]= $tmp;
		}
	}
 
 
	static protected function meetTerminal(&$symbol, &$context, &$index )
	{
		Debug::print_r($symbol);
		if ( count($context) )
		{
			Debug::print_r($context);
			$symbol[$index] = $context;
			$index++;
			$context = array();
			Debug::print_r($symbol);
			return true;
		}
		return false;
	}
 
	static protected function unquoteString($str)
	{
		if ( '"' === $str[0] )
		{
			return substr($str, 1, -1);
		}
		else
		{
			return $str;
		}
	}
}
voilou si je remplace le arrayObject par un array() ça marche bien j'ai la trace suivante:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => item
    [1] => plaspistol_ammo
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => item
            [1] => plaspistol_ammo
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => name
    [1] => _Plasma Pistol Charger
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => name
            [1] => _Plasma Pistol Charger
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => name
            [1] => _Plasma Pistol Charger
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => model
    [1] => weapons/plaspistol/plaspistol_clip
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => name
            [1] => _Plasma Pistol Charger
        )
 
    [1] => Array
        (
            [0] => model
            [1] => weapons/plaspistol/plaspistol_clip
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:127 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => name
            [1] => _Plasma Pistol Charger
        )
 
    [1] => Array
        (
            [0] => model
            [1] => weapons/plaspistol/plaspistol_clip
        )
 
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:130 ( OB:0 )
Array
(
    [0] => type
    [1] => ammo
)
 
Debug::print_r appelé en /var/www/jerikojerk.free.fr/ufo-ai/include/UfoParser.class.php:134 ( OB:0 )
Array
(
    [0] => Array
        (
            [0] => name
            [1] => _Plasma Pistol Charger
        )
 
    [1] => Array
        (
            [0] => model
            [1] => weapons/plaspistol/plaspistol_clip
        )
 
    [2] => Array
        (
            [0] => type
            [1] => ammo
        )
 
)
__________________
PHP fait nativement la validation d'adresse électronique Vous êtes perdu en PHP? rassurez-vous ici (en)
Utilisez le bouton résolu!
gene69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 00h11   #2
Membre Expert
 
Avatar de gene69
 
Inscription : janvier 2006
Messages : 951
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : janvier 2006
Messages : 951
Points : 1 063
Points : 1 063
je pense que j'ai trouvé.. apparament un array() duplique son contenu lors de l'assignement alors que ArrayObject se contente de stocker les références... et comme pas de clone sur les array() je suis obligé d'utiliser des variables temporaires intermédiaires...

quelle histoire...
__________________
PHP fait nativement la validation d'adresse électronique Vous êtes perdu en PHP? rassurez-vous ici (en)
Utilisez le bouton résolu!
gene69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 00h17   #3
Modérateur
 
Inscription : septembre 2010
Messages : 7 103
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 103
Points : 8 456
Points : 8 456
l’accès est le même, c'est pas grave
fait un getArrayCopy si tu veux le même affichage pour ton print_r
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 00h32   #4
Membre Expert
 
Avatar de gene69
 
Inscription : janvier 2006
Messages : 951
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : janvier 2006
Messages : 951
Points : 1 063
Points : 1 063
si c'était problématique pour moi parce que je fais ça (sans la variable intermédiaire):

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	static protected function meetTerminal(&$symbol, &$context, &$index )
	{
		Debug::print_r($symbol);
		if ( count($context) )
		{
			Debug::print_r($context);
			$symbol[$index] = $prevent_by_reference_assignment = $context;
			$index++;
			$context = array();
			Debug::print_r($symbol);
			return true;
		}
		return false;
	}
C'était vraiment pas un pb d'affichage... si tu regardes dans la premiere trace tu as des éléments dans le storage de la classe objectArray (la variable symbol) étaient tous les mêmes à cause de la référence. grrrrr
__________________
PHP fait nativement la validation d'adresse électronique Vous êtes perdu en PHP? rassurez-vous ici (en)
Utilisez le bouton résolu!
gene69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 09h53.


 
 
 
 
Partenaires

Hébergement Web