why i get nothing while extending 2 classes?

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
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

why i get nothing while extending 2 classes?

Post by saumya »

person.php

Code: Select all

<?php
class person{
	private $name;
	private $age;
	function __construct($name,$age){
		$this->name	= $name;
		$this->age = $age;
	}
	function getName(){
		return $this->name;
	}
	function getAge(){
		return $this->age;
	}
}
?>
male.php

Code: Select all

<?php
include "person.php";
class male extends person{
  private $sex = "Male";
  function __construct($name,$age){
    parent::__construct($name,$age);
  }
  function getGender(){
    return ($this->sex);
  }
}
?>
female.php

Code: Select all

<?php
include "person.php";
class female extends person{
  private $sex = "Female";
  function __construct($name,$age){
    parent::__construct($name,$age);
  }
  function getGender(){
    return ($this->sex);
  }
}
?>
manypeople.php

Code: Select all

<?php
include "female.php";
include "male.php";
function dPerson($any){
print($any->getName()." is ".$any->getAge()." years old and is a ".$any->getGender().".<br>");
}
$tara = new female("Tara",28);
dPerson($tara);
$joe = new male("Joe",30);
dPerson($joe);
?>
while i view my "manypeople.php" it shows me nothing, why?but if i remove at leat one class files like male or female then i get the one out put correctly. Any body please help me understand the concept of extends.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

redefinition of class person.

that should be similar to what you'll find in your error logs. Reason why: you have included person.php twice. Either use include_once(), or use the __autoload() magic function, or write person.php to know not to redefine itself.. :)
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

wow.
that worked.I tried include_once and that worked.
thanks a lot.
what the magic function?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Sounds like you may want to turn your error_reporting() up. Use E_ALL while testing / developing.

Cheers
Post Reply