Page 1 of 1

why i get nothing while extending 2 classes?

Posted: Mon Sep 12, 2005 2:26 am
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.

Posted: Mon Sep 12, 2005 2:37 am
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.. :)

Posted: Mon Sep 12, 2005 3:40 am
by saumya
wow.
that worked.I tried include_once and that worked.
thanks a lot.
what the magic function?

Posted: Mon Sep 12, 2005 8:27 am
by feyd

Posted: Mon Sep 12, 2005 11:47 am
by BDKR
Sounds like you may want to turn your error_reporting() up. Use E_ALL while testing / developing.

Cheers