WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Extending & Building Upon PHP5 Classes
Categories : PHP, PHP Classes, Object Oriented Click here to Update Your Picture
Joseph Crawford
Date : Aug 23rd 2005
Grade : 2 of 5 (graded 7 times)
Viewed : 11393
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Joseph Crawford
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
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.



My Box - PHP Class that calculates the volumetric weight of a package.
Categories : PHP, Object Oriented, PHP Classes, Beginner Guides, Payment Gateways
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library
Access_user Class - an easy to use system for protecting pages and register users.
Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication
Football News Aggregator
Categories : PHP, Object Oriented, PHP Classes, Rich Site Summary (RSS), HTML and PHP
SPL and ITERATOR : examples
Categories : PHP, Object Oriented, PHP Classes, Sessions
An updated OOP - Inheritance
Categories : PHP, PHP Classes, Object Oriented
Class to Create protected URLs
Categories : PHP, PHP Classes, URLs
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps
shopping cart class with add/edit/delete product functionality.
Categories : PHP, PHP Classes, Ecommerce
HTML_Graphs provides a simple PHP interface for creating pure HTML charts.
Categories : Graphics, PHP, PHP Classes, Charts and Graphs
very simple ftp class
Categories : PHP, PHP Classes, FTP
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, PHP Classes, Regexps
Menu in sliding bar or tree style. Handles frames by using small amount of javascript. Handles external and internal pages. Allows custom code to replace a menu item.
Categories : PHP Classes, PHP, Java Script, DHTML