PHP Beginning OOP


/ Published in: PHP
Save to your folder(s)

These are important object oriented principles (OOP) that one must understand in PHP to create dynamic web applications. I hope you find this useful in understanding inheritance.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3.  * Author: Alvin Crespo
  4.  * Date: 2/23/2010
  5.  *
  6.  * Employee Class is the parent for the Manager subclass
  7.  *
  8.  * */
  9.  
  10. class Employee{
  11.  
  12. protected $fname; //protected variables and functions are passed to the subclass
  13. protected $lname;
  14. protected $salary = 32000;
  15. protected $position;
  16.  
  17. function __construct($firstname, $lastname, $position){
  18. /*
  19. * Methods pertaining to this class
  20. *
  21. * */
  22. $this->SetFirstName($firstname);
  23. $this->SetLastName($lastname);
  24. $this->SetPosition($position);
  25. }
  26.  
  27. /*
  28. * User Private Information
  29. *
  30. * */
  31. protected function SetLastName($lastname){
  32. $this->lname = $lastname;
  33. }
  34. protected function SetFirstName($firstname){
  35. $this->fname = $firstname;
  36. }
  37. public function GetFirstName(){
  38. return $this->fname;
  39. }
  40. public function GetLastName(){
  41. return $this->lname;
  42. }
  43.  
  44. /*
  45. *User Work Information
  46. *
  47. * */
  48. protected function SetSalary($salary){
  49. $this->salary = $salary;
  50. }
  51. public function GetSalary(){
  52. return "$".$this->salary;
  53. }
  54. protected function SetPosition($position){
  55. $this->position = $position;
  56. }
  57. public function GetPosition(){
  58. return $this->position;
  59. }
  60.  
  61. function __destruct(){
  62.  
  63. }
  64. }
  65.  
  66. ?>
  67.  
  68. <?php
  69.  
  70. /*
  71.  * Author: Alvin Crespo
  72.  * Date: 2/23/2010
  73.  *
  74.  * Class Manager is a sublass to Employee
  75.  *
  76.  * */
  77.  
  78. class Manager extends Employee{
  79.  
  80. function __construct($fname, $lname){
  81. /*
  82. * These methods are located in the parent class Employee
  83. *
  84. * */
  85. $this->SetLastName($lname);
  86. $this->SetFirstName($fname);
  87. $this->SetSalary(48000);
  88. $this->SetPosition('Manager');
  89. }
  90.  
  91. function __destruct(){
  92.  
  93. }
  94. }
  95. ?>
  96.  
  97. /* This is what you would do in your .php file calling these functions*/
  98.  
  99. <?php
  100. require_once 'classes/Employee.php';
  101. require_once 'classes/Manager.php';
  102.  
  103. $employee = new Employee('jack','smith','Copywriter');
  104. echo "First Name: " . $employee->GetFirstName();
  105. echo "<br/>";
  106. echo "Last Name: " . $employee->GetLastName();
  107. echo "<br/>";
  108. echo "Salary: " . $employee->GetPosition();
  109. echo "<br/>";
  110. echo "Salary: " . $employee->GetSalary();
  111.  
  112. echo "<br/><br/>";
  113. $manager = new Manager('frank','josom');
  114. echo "First Name: " . $manager->GetFirstName();
  115. echo "<br/>";
  116. echo "Last Name: " . $manager->GetLastName();
  117. echo "<br/>";
  118. echo "Salary: " . $manager->GetPosition();
  119. echo "<br/>";
  120. echo "Salary: " . $manager->GetSalary();
  121. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.