public function id($id = NULL) {
if(isset($id)) {
if(!is_int($id)) throw new EmployeeEx(__METHOD__.' expects an integer parameter.');
$this->_employee_id = $id;
} else {
return $this->_employee_id;
}
}
public function company($company = NULL) {
if(isset($company)) {
if(!$company instanceof Company) throw new EmployeeEx(__METHOD__.' expects an instance of Company as a parameter.');
$this->_company = $company;
} else {
return $this->_company;
}
}
public function email($email = NULL) {
if(isset($email)) {
if(!is_string($email)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_email = $email;
} else {
return $this->_email;
}
}
}
?>
The employee method extends the person method so that you have access to all of the person methods and properties, you also have access to all method and properties in the Profile class since the Person class extended the Profile class.
Now that we have our Profile, Person and Employee classes setup, we need to have a Company class that will also extend the Profile class.
public function id($id = NULL) {
if(isset($id)) {
if(!is_int($id)) throw new CompnayEx(__METHOD__.' expects an integer parameter.');
$this->_id = $id;
} else {
return $this->_id;
}
}
public function name($name = NULL) {
if(isset($name)) {
if(!is_string($name)) throw new CompanyEx(__METHOD__.' expects a string parameter.');
$this->_name = $name;
} else {
return $this->_name;
}
}
public function url($url = NULL) {
if(isset($url)) {
if(!is_string($url)) throw new CompanyEx(__METHOD__.' expects a string parameter.');
$this->_url = $url;
} else {
return $this->_url;
}
}
}
?>
Now let's see how this would all work ;)
<?php
$joe = new Employee();
$joe->company( new Company() )
$joe->address('1234 PHP Lane.');
$joe->company()->name('Codebowl Solutions');
echo 'Joe lives at '.$joe->address().'.<br>';
echo 'Joe owns '.$joe->company()->name().'.';
?>
now if PHP had allowed for MULTIPLE inheritance we could have made things even easier just by doing something like this.
<?php
// PHP DOES NOT SUPPORT THIS SO DONT TRY IT, THIS IS JUST FOR EXAMPLE
class Employee extends Person, Company {
}
?>
If PHP had allowed for that we wouldnt have had to structure things like we did in the example of how to use these classes, we could have simply inherited the methods and properties for the Company class like we did the Person class.
Who knows, maybe in the future PHP will allow for multiple inheritance that would be awesome ;)
Well I hope someone atleast got something from this example, post comments if you have any questions.