self With Extends Bug?

liunx

Guest
I'm realy going to use this forum as all I'm doing is PHP5 now. :D

Anyway, I think this is a bug or I'm thinking of this the wrong way. Take a look at this example:

<?php
class Test1 {
const VERSION = '0.1';
public static function getVersion()
{ return(self::VERSION); }
}
class Test2 extends Test1 {
const VERSION = '0.2';
}
print(Test2::getVersion()); // Prints 0.1
?>

Ok, now as you can see Test1 has a method getVersion in which gets the self VERSION constant. But, when calling the method from Test2 (which extends Test1) it returns the Test1 VERSION constent. I thought it should return Test2's constent since that's the class it's called from. Dono, maybe someone knows whats going on.. ThanksSince I've not found too much reference material for PHP5's new stuff yet, and it is *not-release-quality* still... this could be a bug...... or is it?

The way I see it, it's behaving normally, but should be chucking an error at you.

Here's why: a constant in pretty much any programming language is meant to be defined *once* and only once. Most languages get angry and error out if you try to change or redefine the value of a constant.

In this example, you're defining the class-level constant and then extending (thus inheriting) all the members of the base class, including that constant. The constant is already set in stone (or should be) when the subclass is instantiated.

I personally think it should give an error in this situation, telling you not to redefine a constant--if you have a token that needs its value to change during the life of the object, a variable-like member would make more sense to me.

Maybe one of these days I'll happen upon a big block of free time (not likely) to go through the ZE2 code and see what's happening.maybe try return __CLASS__::VERSION;? because the function is defined in Test1, i think that self will always refer to Test1 even in an extended class. (unless it's defined in an extended class, of course)Originally posted by Moonglobe
maybe try return __CLASS__::VERSION;
Nope, PHP doesn't print anything then. I think it doesn't like the __CLASS__ var inside a static function.. Any other suggestions?I have done only OO in JAVA application and what I can see is that you are calling a function that is in CLASS1 so how can the class1 can get your version from your class2 :P

Class2 can get all stuff (public) from classe1 but not the reverse.


If you want to get the version from the "child" you need to define a function to get the version of the child.

Hope that helped you.Originally posted by daok
Class2 can get all stuff (public) from classe1 but not the reverse.
I can see where you're going with that. Test1 has no clue that Test2 has the same var. This makes sense but I know there should be a way to look at a var in each class, such as the key work parent. Just trying to save time by not making the function over again in each sub class, which is the reason for extending classes. Arg, I'm going to try a few other things. Anyone else is free to chime in! Any help/ideas are great.When are const definitions bound to the variable, and when are parent class variables looked at?

When you have



class A {
const thing = "other";
protected $var = "stuff";

function __construct()
{

}
}


when is thing bound to "other" and $var bound to be "stuff"? Is it when A is instantiated (ie just before the constructor is called?)?

If that's the case, Test2 is instantiated, and all default variable definitions are looked at and set just before the constructor is called for it. In which case, which order are default variables viewed in? ie, does it look at the child first, then further up the tree to the parent? Which would explain this behaviour (Test2::VERSION being defined, then Test1::VERSION overwriting it).

Either way, I also think it should throw an error about trying to redefine a const :)The constents bind at compile time and the normal var durring construction. goldbug also pointed out the constent being declaired twice but here's my thought as well. I'm not getting an error AND I can access both constens by doing Test1::VERSION Test2::VERSION. I think this is because class constents are like private var's, only that class can get the constent there for you can declare it in each class. So, the issue at this point has something to do with self not extending correctly. Dono for sure. I've tried changing the constents to static vars but then it doesn't like self, self::$version. Any other ideas? Thanks

P.S. - I sent the constents public/private question directly to Zend so I'll let you know what they say.
 
Back
Top