Page 1 of 1

class extension

Posted: Thu May 31, 2007 2:32 pm
by Think Pink
helo, i have a question (stupid maybe)

can i do this ?

Code: Select all

class dummy {
// class content
}

class dummy2 extends dummy {
// class content
}

class dummy3 extends dummy {
// class content
}

class dummy4 extends dummy {
// class content
}

Posted: Thu May 31, 2007 2:46 pm
by superdezign
I believe so. If not, it's the keyword "implements."

(I'm not too familiar with inheritance in PHP yet, but that looks right.)

Posted: Thu May 31, 2007 2:46 pm
by RobertGonzalez
Have you tried it yet?

Posted: Thu May 31, 2007 4:33 pm
by Think Pink
yes i have tried it and is not working apparently

Code: Select all

class x{
	var $vara;
	var $varb;

	function x($var){
		$this->vara = $var;
	}
}


class y extends x{
	function y($var){
		$this->$varb = $this->vara + $var;
	}
}


class z extends x{
	function r(){
		echo $this->$varb;
	}
}


$a = 1;
$b = 2;
$x = new x($a);
$y = new y($b);
$z = new z;
echo $z->r();
i get this error
Warning: Missing argument 1 for x::x() in C:\MySites\test1\test1.php on line 6
Am I doing something wrong?

Posted: Thu May 31, 2007 4:36 pm
by John Cartwright
the parent constructor will not be called unless you explicitely call it from the child.

Code: Select all

class foo extends bar
{
   function foo()
   {
      $this->foo = 'var';
      parent::bar();
   }
}

class bar
{
   function bar()
   {
      $this->bar = 'var';
   }
}

Posted: Thu May 31, 2007 4:38 pm
by feyd
$varb is never initialized.

Posted: Thu May 31, 2007 4:43 pm
by superdezign
Lost the extra '$' in $this->$varb.