| Author |
I fell into a strange trap today. Add non-existent properties to non-existent class!
|
|
| Peter Fox 2005-01-26, 3:56 pm |
| <?php
/*
Making up class properties on the fly!
--------------------------------------
PHP version 4.3.5
An instance of a class can have properties added on the fly.
This behaviour tripped me up when I got my objects a bit mixed up
where $wrongClassInstance->aVar='foo' should have been
$correctClassInstance->aVar='foo'.
No error was reported. PHP happily added aVar to the properties of
$notThisOne being an instance of a class with no $aVar.
Q1: Does this happen in PHP 5?
Q2: Is there a way to trap or turn this off?
*/
class A {
var $foo; // foo is a legitimate variable
function TryThis(){
$this->foo = $this->bar; // bar is a bastard variable (not
declared)
}
}
#=======================================
===============================
$a = new A;
$a->foo = 'foo';
print("FOO $a->foo<br>"); // so far so normal
$a->bar = 'bar'; // making this up as we go along!
print("BAR $a->bar<br>"); // OK!
var_dump($a);
$a->TryThis(); // and this works!
print("<br>??? $a->foo<br>");
#=======================================
===============================
print('<hr>');
$b = new A;
$b->foo='FOO';
$b->TryThis(); // this fails
var_dump($b);
#=======================================
===============================
print('<hr>');
$c = NULL; // now it gets really strange
$c->foo = 'foo'; // this works!!
print("FOO $c->foo<br>");
var_dump($c);
print(phpversion());
?>
--
PETER FOX Not the same since the poster business went to the wall
2 Tees Close, Witham, Essex. private@eminent.demon.co.uk
Gravity beer in Essex <http://www.eminent.demon.co.uk>
| |
| Chung Leong 2005-01-26, 8:55 pm |
|
"Peter Fox" <bin@eminent.demon.co.uk> wrote in message
news:A9LFMhANUr9BFwle@eminent.demon.co.uk...
> <?php
> /*
> Making up class properties on the fly!
> --------------------------------------
>
> PHP version 4.3.5
>
> An instance of a class can have properties added on the fly.
>
> This behaviour tripped me up when I got my objects a bit mixed up
> where $wrongClassInstance->aVar='foo' should have been
> $correctClassInstance->aVar='foo'.
> No error was reported. PHP happily added aVar to the properties of
> $notThisOne being an instance of a class with no $aVar.
>
> Q1: Does this happen in PHP 5?
> Q2: Is there a way to trap or turn this off?
>
>
> */
In PHP5 you can add a __set() and a __get() method to track access to
undeclared properties.
As far as I know there's isn't a way you can make PHP spit out an error or a
warning when an undeclared property is set.
| |
|
| On Tue, 25 Jan 2005 21:07:25 +0000, Peter Fox
<bin@eminent.demon.co.uk> wrote:
> An instance of a class can have properties added on the fly.
That's standard behavour. In fact, for PHP4 I almost never declare
class properties for that reason. Properties are just regular
variables in this way; you can create them just by assigning to them.
PHP5 has _get() and _set() which will allow you to raise an exception
or error on undeclared properties. I also believe PHP5 also does not
do automatic promotion to objects -- which would prevent your second
example:
$a = NULL;
$a->Var = 'hello'
This won't work in PHP5 because $a won't be automatically converted
into an object from null. This auto conversion is an old feature from
the PHP3 days where you could create an object just by assigning a
property to an undeclared (null) variable.
|
|
|
|