Page 1 of 1

inheritance question

Posted: Mon Mar 17, 2003 7:59 am
by Guy
I have classes inheritance something like this small example

Code: Select all

<?PHP
class A
&#123;
	function A()
	&#123;
		$this->dosomething();
	&#125;
	
	function dosomething()
	&#123;
		echo "in A<br>";
	&#125;
&#125;

class B extends A
&#123;
	function B()
	&#123;
		$this->A();      //will print 'in B'
		$this->dosomething();   //will print 'in B'
		A::A();    //will print 'in B'
		A::dosomething(); //will print 'in A'
		
	&#125;
	
	function dosomething()
	&#123;
		echo "in B<br>";
	&#125;
&#125;

$b = new B;

?>
look at the comment in the constructor of B, i want (if there is a away )
to call the constructor of A from B and still call dosomthing function of A and not B.
can it be done?
Thanks
Guy

Posted: Mon Mar 17, 2003 12:34 pm
by protokol
Since the constructor in A simply calls the function dosomething() from A, then you can just call this function from B:

A::dosomething();

Since you redefine dosomething() in B, the constructor of A::A() will be calling the B::dosomething(). This is just how inheritance works and there's no real way to get around it.

Posted: Mon Mar 17, 2003 5:14 pm
by bionicdonkey
since A is the parent class of B use would use:

parent::dosomething();