|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
Ok so i am going to show another example of how to easilly extend objects in php 5 to save writing duplicate code.
This is my main profile class, this is used for several things, notice it only has methods and properties for address, city, state, etc.. Read on ;)
| <?php
class Profile {
protected $_address1;
protected $_address2;
protected $_city;
protected $_state;
protected $_zip;
protected $_phone;
protected $_fax;
public function __construct() {
}
public function address1($address = NULL) {
if(isset($address)) {
if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_address1 = $address;
} else {
return $this->_address1;
}
}
public function address2($address = NULL) {
if(isset($address)) {
if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_address2 = $address;
} else {
return $this->_address2;
}
}
public function city($city = NULL) {
if(isset($city)) {
if(!is_string($city)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_city = $city;
} else {
return $this->_city;
}
}
public function state($state = NULL) {
if(isset($state)) {
if(!is_string($state)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_state = $state;
} else {
return $this->_state;
}
}
public function zip($zip = NULL) {
if(isset($zip)) {
if(!is_string($zip)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_zip = $zip;
} else {
return $this->_zip;
}
}
public function phone($phone = NULL) {
if(isset($phone)) {
if(!is_string($phone)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_phone = $phone;
} else {
return $this->_phone;
}
}
public function fax($fax = NULL) {
if(isset($fax)) {
if(!is_string($fax)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
$this->_fax = $fax;
} else {
return $this->_fax;
}
}
}
?> | |
Now that we have our profile class we can create a few more classes. Person, Employee, Company.
The person class specifies that the object represents well you guessed it, a person ;) Look at the person class.
| <?php
class Person extends Profile {
protected $_fname;
protected $_lname;
public function __construct() {
}
public function firstName($fname = NULL) {
if(isset($fname)) {
if(!is_string($fname)) throw new PersonEx(__METHOD__.' expects a string parameter.');
$this->_fname = $fname;
} else {
return $this->_fname;
}
}
public function lastName($lname = NULL) {
if(isset($lname)) {
if(!is_string($lname)) throw new PersonEx(__METHOD__.' expects a string parameter.');
$this->_lname = $lname;
} else {
return $this->_lname;
}
}
}
?> | |
Notice that the person class only has properties and methods for the persons first and last name, the rest will be covered in the Employee object.
| <?php
class Employee extends Person {
protected $_employee_id;
protected $_company;
protected $_email;
public function __construct() {
}
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.
| <?php
class Company extends Profile {
protected $_id;
protected $_name;
protected $_url;
public function __construct() {
}
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. |
|
| SPL and ITERATOR : examples Categories : PHP, Object Oriented, PHP Classes, Sessions | | | Advanced Image WaterMarker Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | An updated OOP - Inheritance Categories : PHP, PHP Classes, Object Oriented | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | |
|
|
|