« Occurrence of a word in a sentence using php


OOP In PHP (Object Oriented Programming in PHP)

Posted by chaabant on Mar 28, 2008

This is a basic introduction on how to use OOP - Oriented Programming In php

Declaring a Class

  1. class myClass {
  2. // Class contents go here
  3. }

Instantiating an Object

  1. $myClassInst = new myClass();

OOP Class Inheritance

  1. class a {
  2.  
  3. function test()
  4. {
  5. echo "a::test called";
  6. }
  7.  
  8. function func()
  9. {
  10. echo "a::func called";
  11. }
  12. }
  13.  
  14.  
  15. class b extends a {
  16.  
  17. function test()
  18. {
  19. echo "b::test called";
  20. }
  21. }
  22.  
  23. class c extends b {
  24.  
  25. function test()
  26. {
  27. parent::test();
  28. }
  29.  
  30. }
  31.  
  32. class d extends c {
  33.  
  34. function test()
  35. {
  36. b::test();
  37. }
  38.  
  39. }
  40.  
  41. $a = new a();
  42. $b = new b();
  43. $c = new c();
  44. $d = new d();
  45.  
  46. $a->test(); // Outputs "a::test called"
  47. $b->test(); // Outputs "b::test called"
  48. $b->func(); // Outputs "a::func called"
  49. $c->test(); // Outputs "b::test called"
  50. $d->test(); // Outputs "b::test called"
Leave a Reply

You must be logged in to post a comment.