pbm using parent:: in child class

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
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

pbm using parent:: in child class

Post by eskio »

Hi, I have a problem using oop features in php.
I want to use some "employee" class properties and methods in the "payroll" class (child class).

employee.php file:

Code: Select all

<?php
class employee extends dbConnect implements IEmployee{

	protected $employee_id;
	
	public function set_employee_id($var){
		$this->employee_id = $var;
	} // set_employee_id($input_var)
	
	public function get_employee_id(){
		return $this->employee_id;
	} // get_employee_id()
	
} // class employee extends dbConnect implements IEmployee
?>
payroll.php file:

Code: Select all

<?php
include_once 'employee.php';

class payroll extends employee {
	
	protected $t_income;
	
	public function get_parent_employee_id(){
		return parent::get_employee_id();
	} // get_parent_employee_id()
	
} // class payroll extends employee
?>
view.php file:

Code: Select all

<?php
include_once 'employee.php'; 
$Emp = new employee;

include_once 'payroll.php'; 
$Payroll = new payroll; 

$Emp->set_employee_id($_GET['ID']);
print '1 - ID: '.$Emp->get_employee_id().'<br>';
print '2 - ID: '.$Payroll->get_parent_employee_id().'<br>';
?>
output is :
1 - ID: 4
2 - ID:

I don't understand why the parent::get_employee_id() doesn't work in the "payroll" class?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: pbm using parent:: in child class

Post by requinix »

It's working fine: you never set the ID for that $Payroll object.

Yes, you did set it for the other $Emp, but the two variables have nothing to do with each other.
If I have two apples and I take a bite out of one, that has absolutely no bearing on whether the other has a bite taken out of it too.
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

Re: pbm using parent:: in child class- SOLVED

Post by eskio »

thanks tasairis.
Post Reply