class extension

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

class extension

Post 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
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried it yet?
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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';
   }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$varb is never initialized.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Lost the extra '$' in $this->$varb.
Post Reply