PHP 5.0 I need a test

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

PHP 5.0 I need a test

Post by alex.barylski »

Can someone please test the following and tell me what happens:

Code: Select all

class CParent{
  function test()
  {
    echo get_class($this).'<br />';
  }
}

class CChild extends CParent{
  function test()
  {
    echo get_class($this).'<br />';
  }
}

$obj = new CChild();
$obj->test();
Does it output:

Code: Select all

CChild
CChild

or

CChild
CParent
Can someone try this code and tell me what happens??? Fixing silly syntax errors if they exist, it's the general idea i'm after :P

Cheers and Thanks alot :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Trick question. It'll do neither. :P

You've only called test() in CChild.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It should just output "CChild"
(#10850)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

And as some stickler for detail pointed out recently Cparent::test() is overridden by Cchild::test() -- NOT overloaded.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:Trick question. It'll do neither. :P

You've only called test() in CChild.
Doh!!! :oops:

Not a trick question, I just forgot something: :P

CChild::test() is supposed to call parent::test()

I'm curious to see how PHP 5 object model deals with $this pointer inside parent functions.

In my version: 4.2.3 PHP object model creates only a single object instance for any number of parent classes derived from.

What this means is, if I need the name of a class dynamically, say at the root class which is 3 classes deep, I'm SOL because get_class($this) returns the name of the aggregate object, regardless of where I am in the hierarchy I get this same result.

__CLASS__ was introduced only in 4.3.0 so my version ain't there yet :? and I like to develop for the lowest common denominator :)

Thus the curiosity of how PHP5 handles this scenario :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

parent::test() or anything else should give "CChild" because that is the class.
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want the ancestry of a class

Code: Select all

<?php
function get_ancestors ($class) {
        
     for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
     return $classes;
    
}
?>
was posted in the comments of get_parent_class()
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

arborint wrote:And as some stickler for detail pointed out recently Cparent::test() is overridden by Cchild::test() -- NOT overloaded.
What does that have to do with anything? :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Nomenclature
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:If you want the ancestry of a class

Code: Select all

<?php
function get_ancestors ($class) {
        
     for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
     return $classes;
    
}
?>
was posted in the comments of get_parent_class()
I'm already using that function :P

But thats not the problem...

I've already figured out a solution, but incase someone can think of a better way...

How do I, inside a class hierarchy, determine a classes name?

I know that whilst writing a method for a function you explicitly know the class in which your working, so the answer is obvious, but in this case, I cannot hardcode the class name - I need something like __CLASS__ but it's not availble in PHP 4.2.3 :(

Any ideas or suggestions?

Cheers :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

arborint wrote:Nomenclature
Not sure of the meaning on that word...BRB while I visit dictionary.com :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you strictly write only one class per file (and they're similarly named) you could use __FILE__. Although kludgy, it's the only thing I can think of right now that'd work back that far.
Post Reply