Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
// 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;
}
}
// 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
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.
<?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
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
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