Re: [PHP4BETA] Classes

From: Date: Mon, 22 Nov 1999 12:24:21 +0000
Subject: Re: [PHP4BETA] Classes
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-6650@lists.php.net to get a copy of this message
CC>> $parent = new ParentClass(); {-------1-------} CC>> CC>> Class ParentClass { CC>> var $somevalue; CC>> var $child; CC>> var $temp; CC>> function ParentClass() CC>> { CC>> $this->somevalue = 1; CC>> $this->child = new ChildClass(); CC>> $this->temp = 3; CC>> } CC>> } CC>> CC>> Class ChildClass { CC>> var $othervalue; CC>> function ChildClass() CC>> { CC>> global $parent; CC>> $this->othervalue = $parent->somevalue + 1; {2} CC>> } CC>> } CC>> CC>> When you'd execute this code, the parent AND $parent->child classes are CC>> both created just fine. (not even a php warning), but the value assigned CC>> to child->othervalue will be 1 .. not 2, since the global $parent wont CC>> exsist yet, its /being/ created, but not assigned to the global CC>> variable yet. The strane point here is that you can do more than just this - you can really assign $parent->anything, though $parent itself has no class. I feel it's a bit strange - member of what exactly is $parent->anything? CC>> The problem i have with this is, that for one its inconsistent, if a CC>> member doesnt exsist of a class, then a value shouldnt exsist either, or In this particular case, the member cannot exist or not exist - because there's no class! But the effect seems to go from Zend' defining variables "on demand". CC>> What i'd prefer to see though, is a C++ / OO Pascal style of variable CC>> handling ... if you could code the above example in C++ or OO Pascal, CC>> the global $parent would exsist. CC>> also $parent->somevalue would be assigned (is already alocated AND CC>> assigned), CC>> also the child class would excist (eg memory for object has been CC>> alocated!), No. If you do exactly this in C++ (don't know about Pascal) you would probably get segmentation fault from using value of parent before it was assigned. IIRC, C++ evaluates expression (fully) and *then* assigns the result. In the point {2} global parent isn't initialized yet, since expression {1} didn't finish yet to evaluate. So you get to dereference whatever trash was stored in parent beforehand. CC>> it would return NULL / false, etc, since this hasnt been CC>> created/initialized/defined yet at the time the child is created.... it In C++, if you don't initialize value, you get *problems*, not NULL/false. C++ makes shooting oneself in the foot a very easy task. CC>> So is there a specific reason for handeling it the way we currently do, CC>> is is this a miss feature? This is a misbehaviour on the side of user, rather then misfeature. You shouldn't do such things in constructor. But this behaviour (defining "on demand" not only class variables, but the class itself) is indeed a bit unusual. -- Stanislav Malyshev stas@zend.com 050-624945

« previous php.version4 (#6650) next »