Page 1 of 1
PHP 5.0 I need a test
Posted: Wed Mar 29, 2006 6:35 pm
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:
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
Cheers and Thanks alot

Posted: Wed Mar 29, 2006 6:41 pm
by feyd
Trick question. It'll do neither.
You've only called test() in CChild.
Posted: Wed Mar 29, 2006 6:42 pm
by Christopher
It should just output "CChild"
Posted: Wed Mar 29, 2006 6:53 pm
by Christopher
And as some stickler for detail pointed out recently Cparent::test() is overridden by Cchild::test() -- NOT overloaded.
Posted: Wed Mar 29, 2006 7:03 pm
by alex.barylski
feyd wrote:Trick question. It'll do neither.
You've only called test() in CChild.
Doh!!!
Not a trick question, I just forgot something:
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

Posted: Wed Mar 29, 2006 9:06 pm
by Christopher
parent::test() or anything else should give "CChild" because that is the class.
Posted: Wed Mar 29, 2006 9:15 pm
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()
Posted: Wed Mar 29, 2006 10:16 pm
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?

Posted: Wed Mar 29, 2006 10:20 pm
by Christopher
Nomenclature
Posted: Wed Mar 29, 2006 10:23 pm
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
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

Posted: Wed Mar 29, 2006 10:26 pm
by alex.barylski
arborint wrote:Nomenclature
Not sure of the meaning on that word...BRB while I visit dictionary.com

Posted: Wed Mar 29, 2006 10:34 pm
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.