To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Upgrading PHP

Upgrading PHP Issues concerning PHP version upgrades and future releases

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-21-2004, 05:08 PM   #1
rpanning
Tandem Web Works
 
rpanning's Avatar
 
Join Date: Dec 2002
Location: MN, USA
Posts: 444
self With Extends Bug?

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

Anyway, I think this is a bug or I'm thinking of this the wrong way. Take a look at this example:
PHP Code:
<?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.. Thanks
__________________
Are you a mountain biker and live in Minnesota? Join MORC to help support our trials!
rpanning is offline   Reply With Quote
Old 05-21-2004, 05:25 PM   #2
goldbug
50,000 Watts of Goodwill
 
goldbug's Avatar
 
Join Date: May 2003
Location: Rummaging through your garbage.
Posts: 1,326
Since 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.
__________________
Many eyes make few mistakes

goldendance

Last edited by goldbug; 05-21-2004 at 05:27 PM.
goldbug is offline   Reply With Quote
Old 05-21-2004, 08:47 PM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
maybe try
PHP Code:
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)
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 05-24-2004, 10:40 AM   #4
rpanning
Tandem Web Works
 
rpanning's Avatar
 
Join Date: Dec 2002
Location: MN, USA
Posts: 444
Quote:
Originally posted by Moonglobe
maybe try
PHP Code:
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?
__________________
Are you a mountain biker and live in Minnesota? Join MORC to help support our trials!
rpanning is offline   Reply With Quote
Old 05-24-2004, 05:10 PM   #5
daok
Member
 
Join Date: May 2003
Posts: 88
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.
daok is offline   Reply With Quote
Old 05-24-2004, 06:30 PM   #6
rpanning
Tandem Web Works
 
rpanning's Avatar
 
Join Date: Dec 2002
Location: MN, USA
Posts: 444
Quote:
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.
__________________
Are you a mountain biker and live in Minnesota? Join MORC to help support our trials!
rpanning is offline   Reply With Quote
Old 05-27-2004, 02:05 AM   #7
Tanus
Junior Member
 
Join Date: Sep 2002
Posts: 14
When are const definitions bound to the variable, and when are parent class variables looked at?

When you have

PHP Code:

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
__________________
WebPanels for PHP
Tanus is offline   Reply With Quote
Old 05-27-2004, 02:36 AM   #8
rpanning
Tandem Web Works
 
rpanning's Avatar
 
Join Date: Dec 2002
Location: MN, USA
Posts: 444
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

[edit]P.S. - I sent the constents public/private question directly to Zend so I'll let you know what they say.[/edit]
__________________
Are you a mountain biker and live in Minnesota? Join MORC to help support our trials!

Last edited by rpanning; 05-27-2004 at 03:30 AM.
rpanning is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 11:48 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.