Classes
| From: | Chris Chabot | Date: | Mon, 22 Nov 1999 12:02:12 +0000 |
| Subject: | Classes | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-6649@lists.php.net to get a copy of this message | ||
A quick question about classes in PHP4 ...
Suppose i have a parent class and a child class :
$parent = new ParentClass();
Class ParentClass {
var $somevalue;
var $child;
var $temp;
function ParentClass()
{
$this->somevalue = 1;
$this->child = new ChildClass();
$this->temp = 3;
}
}
Class ChildClass {
var $othervalue;
function ChildClass()
{
global $parent;
$this->othervalue = $parent->somevalue + 1;
}
}
When you'd execute this code, the parent AND $parent->child classes are
both created just fine. (not even a php warning), but the value assigned
to child->othervalue will be 1 .. not 2, since the global $parent wont
exsist yet, its /being/ created, but not assigned to the global
variable yet.
Now suppose we did this with a function (eg it wouldnt be a var
$somevalue but a function somevalue()), then the PHP interpreter would
issue an error/warning, that your refering a non exsistent member
($parent->somevalue()).
The problem i have with this is, that for one its inconsistent, if a
member doesnt exsist of a class, then a value shouldnt exsist either, or
the function reference should always return Null ... this way the way
'non defined class references' are handled is two fold ....
What i'd prefer to see though, is a C++ / OO Pascal style of variable
handling ... if you could code the above example in C++ or OO Pascal,
the global $parent would exsist.
also $parent->somevalue would be assigned (is already alocated AND
assigned),
also the child class would excist (eg memory for object has been
alocated!),
but if we would refer to $parent->temp in the child class constructor,
it would return NULL / false, etc, since this hasnt been
created/initialized/defined yet at the time the child is created.... it
wouldnt return an error though, since it is a valid specified token...
it just hasnt been defined what its type is, and/or its value.... Now if
we would refer to $parent->invalid_param this could/should create a
warning/error since its not a part of the objects VMT (virtual method
table)/stack...
So is there a specific reason for handeling it the way we currently do,
is is this a miss feature?
-- Chris Chabot