Example of a php abstract class to learn how to implement a php abstract class-php Tutorial

Source: Internet
Author: User
Example of a php abstract class to learn how to implement a php abstract class

  1. // Define an abstract class

  2. Abstract class Staff
  3. {
  4. Abstract function hire ();
  5. Abstract function fire ();
  6. Abstract function promote ();
  7. Abstract function demote ();
  8. }

  9. ?>

Example 2: php abstract class

  1. Class Employee {

  2. Private $ title;
  3. Private $ lastName;
  4. Private $ firstName;
  5. Protected $ salary;
  6. Private $ ratio = 0;
  7. Public function _ construct ($ title, $ firstName, $ mainName, $ salary ){
  8. $ This-> title = $ title;
  9. $ This-> firstName = $ firstName;
  10. $ This-> lastName = $ mainName;
  11. $ This-> salary = $ salary;
  12. }

  13. Public function firstName (){

  14. Return $ this-> firstName;
  15. }

  16. Public function getlastName (){

  17. Return $ this-> lastName;
  18. }

  19. Public function setRatio ($ num ){

  20. $ This-> ratio = $ num;
  21. }

  22. Public function getRatio (){

  23. Return $ this-> ratio;
  24. }
  25. Public function getTitle (){
  26. Return $ this-> title;
  27. }

  28. Public function getSalary (){

  29. Return ($ this-> salary-$ this-> ratio );
  30. }

  31. Public function getFullName (){

  32. Return "{$ this-> firstName}". "{$ this-> lastName }";
  33. }

  34. Function getSummaryLine (){

  35. $ Base = "$ this-> title ($ this-> lastName ,";
  36. $ Base. = "$ this-> firstName )";
  37. Return $ base;
  38. }
  39. }

  40. // Define an abstract class

  41. Abstract class EmployeeWriter {
  42. Abstract static function write (Employee $ shopProduct );
  43. }

  44. Class TextEmployeeWriter extends EmployeeWriter {

  45. Static function write (Employee $ shopEmployee ){
  46. $ Str = "{$ shopEmployee-> getTitle ()}:";
  47. $ Str. = $ shopEmployee-> getFullName ();
  48. $ Str. = "({$ shopEmployee-> getSalary ()}) \ n ";
  49. Print $ str;
  50. }
  51. }

  52. $ Employee 1 = new Employee ("A", "A1", "A2", 5.99 );

  53. TextEmployeeWriter: write ($ interval 1 );
  54. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.