Why is it possible to assign data to _not_declared_ vars in a class (PHP 5.0.3)?

From: Date: Fri, 08 Apr 2005 16:24:30 +0000
Subject: Why is it possible to assign data to _not_declared_ vars in a class (PHP 5.0.3)?
Groups: php.general 
Request: Send a blank email to php-general+get-212651@lists.php.net to get a copy of this message
Hello All, Why is this working? <?php class foobar { public
       $a,
       $b;
public function __construct() {
      $this->a = "Hello ";
      $this->b = "world! ";
      $this->c = "Good bye... ";
} public function foo() {
       echo $this->a."<br>";
       echo $this->b."<br>";
       echo $this->c."<br>";        }
} ?> CALL: $test = new foobar(); $test->foo(); OUTPUT: Hello world Good bye... If i understand right, all variables should be declared in PHP5. So why is it possible to add a membervariable called "c" to the object without making a declaration? I got no error with this. I thought E_STRICT should show me things like that. Could someone explain me that? I found this problem by building one dynamic get and set method for all member variables with the help of the interceptor method __call() . In this method i originally has build a try catch block that looks like this: <example_code> try { $this->non_existent_member = "FooBar"; } catch (Exception $e) { echo "The variable 'non_existent_member' is not declared!"; } </example_code> I expected that the try block should catch the ERROR run the catch block but this doesn't heppend and and took some time to figure that out. I think it is really bad, that this kind of logical error must be catched in some way or must be at least reported with E_STRICT on. I am only develping with E_STRICT and thought this will give hints to all design faults. Kind regards Johannes Findeisen P.S.: I am going crazy ... This works too: <?php class foobar { public
       $a,
       $b;
var $bla; public function __construct() {
      $this->a = "Hello ";
      $this->b = "world! ";
      $this->c = "Good bye... ";
} public function foo() {
       echo $this->a."<br>";
       echo $this->b."<br>";
       echo $this->c."<br>";        }
} class failover { public
       $foobar;
public function __construct() {
       $this->foobar = new foobar();
       $this->foobar->d = "...all Aliens! ";
     }
     public function bar() {
         echo $this->foobar->a."<br>";
       echo $this->foobar->b."<br>";
       echo $this->foobar->c."<br>";
       echo $this->foobar->d."<br>";
     }
} ?> CALL: $test = new foobar(); $test->foo(); $test2 = new failover(); $test2->bar(); OUTPUT: Hello world! Good bye... Hello world! Good bye... ...all Aliens!

« previous php.general (#212651) next »