<?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. pr*****@eminent.demon.co.uk
Gravity beer in Essex <http://www.eminent.demon.co.uk>