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

Your name:

Reply:


Posted by Johannes Findeisen on 04/08/05 19:24

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!

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация