Problem with __get and arrays

Alles, was PHP betrifft, kann hier besprochen werden.

Problem with __get and arrays

Postby ordure » 04. March 2007 05:16

Hi, Since Xampp 1.5.5 (1.6.0a is the same) my programs does not work anymore. They where working under 1.5.2 !!!

Doing tests, I've found a little buged program.
Can somebody help me ???

The final result becomes (
    FIN REMPLIR ==>
instead of
    FIN REMPLIR ==>Array ( [1] => toto )
.

Regards,
Thierry.

Code: Select all
<?php

$foo = new bidon();

class bidon {
   private $_proprietes = array('_t');

   public function __construct() {
      foreach ($this->_proprietes as $propriete) $this->$propriete = null;
      $this->remplir();
   }

   public function __set($nom, $valeur ) {
      if (!in_array($nom, $this->_proprietes, true)) {
         throw new Exception('propriete ' .$nom .' INCONNUE pour objet ClassAllMembres');
      }
      $this->_proprietes[$nom] = $valeur;
   }

   public function __get($nom) {
      if (!in_array($nom, $this->_proprietes, true)) {
         throw new Exception('propriete ' .$nom .' INCONNUE pour objet bidon');
      }
      return $this->_proprietes[$nom];
   }

   private function remplir() {
      $this->_t [1] = 'toto';
      print "<br>FIN REMPLIR ==>";
      print_r($this->_t);
   }
}
?>
ordure
 
Posts: 6
Joined: 04. March 2007 05:11

Postby Wiedmann » 04. March 2007 14:06

Code: Select all
public function __get($nom) {

For example you can change this line to:
Code: Select all
public function &__get($nom) {
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Problem with __get and arrays

Postby ordure » 04. March 2007 16:03

Good try, but it does not works.
ordure
 
Posts: 6
Joined: 04. March 2007 05:11

Postby Wiedmann » 04. March 2007 19:02

but it does not works.

Well, with this change, the above script works here with all PHP versions...
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby ordure » 04. March 2007 19:43

Hmmmm, Hmmm,

I've test it ... but it was not working.

Maybe, I've made a miss.

I will try again, and will keep you in touch.
ordure
 
Posts: 6
Joined: 04. March 2007 05:11

Postby ordure » 04. March 2007 19:51

I've test it again ... but same result.

Result occured with [2006-11-28] XAMPP for Linux 1.5.5a (PHP 5.2.0).

:shock:
ordure
 
Posts: 6
Joined: 04. March 2007 05:11

Postby Wiedmann » 04. March 2007 20:18

works here with all PHP versions...

Result occured with [2006-11-28] XAMPP for Linux 1.5.5a (PHP 5.2.0).

sorry, my mistake. I've test this with PHP 5.0.x, 5.1.x and 5.2.1. But not with 5.2.0.

(5.2.0 have really many bugs... see the changelog)
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

[Resolved]

Postby ordure » 04. March 2007 23:25

Yaouhhh,

I've upgraded to 5.2.1 and inserted your modification (&__get) ...
and it works.

Thanks a lot.
ordure
 
Posts: 6
Joined: 04. March 2007 05:11

Postby ordure » 04. March 2007 23:41

Hi,

another solution found in another forum is to modify the constructor.

Code: Select all
public function __construct() {
         foreach ($this->_proprietes as $propriete) $this->$propriete = [b]new ArrayObject();[/b]
               $this->remplir();
      }




It works fine ... and without the &__get.

Regards.
ordure
 
Posts: 6
Joined: 04. March 2007 05:11


Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests