Page 1 of 1
accessing extended class functions
Posted: Fri Nov 25, 2005 3:23 am
by malcolmboston
eg.
Code: Select all
// i know this class hs problems, i just wanted an example
class A
{
function A ()
{
// legacy compatability (constructor)
$this->Data->A = $a;
}
function ExecuteA ()
{
print $this->Data->A;
}
}
class B extends A
{
function B ()
{
$this->Data->B = $b;
}
function ExecuteB ()
{
print $this->Data->B;
}
}
ok to access ExecuteA i could do
Code: Select all
// if someone can give me some pointers on advantages / disadvantages of each method, would be much appreciated
$Class = new A;
$Class->ExecuteA();
//or
A::ExecuteA();
my question is how could i access a function in an extended class, i cant seem to get the syntax correct even though logically it seems right to me
Posted: Fri Nov 25, 2005 3:46 am
by Maugrim_The_Reaper
You need to instantiate the actual extension - not the parent.
Just to note - where is the Data property coming from? It's not defined in either class, you could mean simple $this->var, not $this->Data->var, unless you intended to create the class property without originally defining it as a property? If so, I'd actually avoid that route.
so...
Code: Select all
<?php
class A
{
var $A;
function A ($a)
{
// legacy compatability (constructor)
$this->A = $a;
}
function ExecuteA ()
{
print $this->A;
}
}
class B extends A
{
var $B;
function B ($b, $a=null)
{
$this->B = $b;
parent::A($a); // or $this->A = $a; (B inherits A's properties)
}
function ExecuteB ()
{
print $this->B;
}
}
$B = new B('B Value');
$B->ExecuteB(); //echo 'B Value'
$B->ExecuteA(); // echo nothing ('A' property is null)
$B2 = new B('B Value', 'A Value');
$B2->ExecuteB(); // echo 'B Value'
$B2->ExecuteA(); // echo 'A Value' - the B constructor calls A parent contructor and sets parent A prop to 'A Value'
?>
Changed a few things to try and show a little more sense in how this could work
Posted: Fri Nov 25, 2005 3:58 am
by malcolmboston
the class was an example as i dont have the classes i am actually putting this into available right now, and i cba typing out a long class.
i guess i was asking if i would have to instantiate it, i was hoping not as its more lines of code to write and i may need to instantiate extended class from within parent as im trying to get a nice syntax for, example i have built a code comparision class which basically compares and contrasts 2 code snippets and breaks them down to a performance level, basically showing efficiency, code calls etc etc, now i have several functions in there which from the parent actually calls extended functionality, im guessing this probably isnt the way it should be done but it keeps my syntax clean.eg
Code: Select all
// i do
$performance = new Performance;
$Performance->Analyze($output = TRUE);
// instead of
$performance = new Performance;
$start = $Performance->Start();
$start = $Performance->End();
$analysis= $Performance->Analyze($start, $end);
$results = $Performance->ShowAnalysis($analysis);
Posted: Fri Nov 25, 2005 4:52 am
by Jenk
If the use of a parent class instantiates an object of a child (extension) class, then they shouldn't be seprated.

Posted: Fri Nov 25, 2005 5:10 am
by malcolmboston
yeah, i see where your coming from, i actually had all of the functions seperated so the CodePerformance class was an extended class of performance, the reason i changed back was because i could not get the syntax correct to seperate the 2...thanks for your help