Object Oriented Programming


This tutorial will teach you OOP (Object Oriented Programming) in PHP. You should have basic PHP knowledge before reading this tutorial. First I will teach you how to make a class, and use it within PHP.

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable, will be set to a value later
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to print out the variable
  14. //
  15. function myfunction() {
  16. print($this->variable);
  17. }
  18.  
  19. //
  20. // End the class
  21. //
  22. }
  23. ?>


This will create a class, but will not really do anything until you build it later. Now, for a little explanation, the variables set in classes need not be set to a value, because later in the script you can set it from outside the class block. You may have of noticed the $this->variable statement. When using classes, you use relation to that class, and variables you want to use while you have code in the class, you can use the $this->{VARIABLENAME}. You may have of also noticed that there is no $ in front of variable, classes don't work like that. Now I can show you how to build a class from outside, and print out the variable.

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable, will be set to a value later
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to print out the variable
  14. //
  15. function myfunction() {
  16. print($this->variable);
  17. }
  18.  
  19. //
  20. // End the class
  21. //
  22. }
  23.  
  24. //
  25. // Create the object
  26. //
  27. $build=new ClassName;
  28.  
  29. //
  30. // Set the variable within the object (notice no $ in front of variable)
  31. //
  32. $build->variable="Hello, this is some text.";
  33.  
  34. //
  35. // Call the function from within the object
  36. //
  37. $build->myfunction();
  38. ?>


This will print out: Hello, this is some text.

The $build variable (after creating the object through it) now relates everything to it with the class, so you could also do it like this:

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable, will be set to a value later
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to print out the variable
  14. //
  15. function myfunction() {
  16. print($this->variable);
  17. }
  18.  
  19. //
  20. // End the class
  21. //
  22. }
  23.  
  24. //
  25. // Create the object
  26. //
  27. $build=new ClassName;
  28.  
  29. //
  30. // Set the variable within the object (notice no $ in front of variable)
  31. //
  32. $build->variable="Hello, this is some text.";
  33.  
  34. //
  35. // Call the variable from within the object
  36. //
  37. echo $build->variable;
  38. ?>


Will print out the same thing as the previous example. For a better understanding of the functions inside of a class, here is an example of some math:

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable, will be set to a value later
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to print out the variable
  14. //
  15. function add($varA,$varB) {
  16. $this->variable=$varA+$varB;
  17. print($this->variable);
  18. }
  19.  
  20. //
  21. // End the class
  22. //
  23. }
  24.  
  25. //
  26. // Build the object
  27. //
  28. $build=new ClassName;
  29.  
  30. //
  31. // Call to the function within the object
  32. //
  33. $build->add(5,5);
  34. ?>


As you probably may have guessed, this will print out 10. Just like a normal function in PHP, except called by the object using the $build variable, then stores the answer in the $variable variable. This means you can do this:

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable, will be set to a value later
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to print out the variable
  14. //
  15. function add($varA,$varB) {
  16. $this->variable=$varA+$varB;
  17. print($this->variable);
  18. }
  19.  
  20. //
  21. // End the class
  22. //
  23. }
  24.  
  25. //
  26. // Build the object
  27. //
  28. $build=new ClassName;
  29.  
  30. //
  31. // Call to the function within the object
  32. //
  33. $build->add(5,5);
  34.  
  35. //
  36. // Call the variable, which now has the value of 5+5 in it
  37. //
  38. echo "".$build->variable;
  39. ?>


This will print out:
10
10

Now, you can use the extends method in OOP, so one class extends into another class, obtaining all of it's functions, variables, etc..

PHP Code
  1. <?php
  2. //
  3. // Build the class, replace ClassName, with the name of your class
  4. //
  5. class ClassName {
  6.  
  7. //
  8. // Set a variable
  9. //
  10. var $variable;
  11.  
  12. //
  13. // Create a function to do something
  14. //
  15. function dosomething() {
  16. //
  17. // Reset the variable to a new value (change e's to 1's)
  18. //
  19. $this->variable=ereg_replace("e","3",$this->variable);
  20.  
  21. //
  22. // Print out the variable
  23. //
  24. print($this->variable);
  25. }
  26.  
  27. //
  28. // End class
  29. //
  30. }
  31.  
  32. //
  33. // Build the class, you can rename it, nothing has to be put in here
  34. //
  35. class DiffClassName extends ClassName {
  36.  
  37. //
  38. // End the class
  39. //
  40. }
  41.  
  42. //
  43. // Build the DiffClassName object
  44. //
  45. $build=new DiffClassName;
  46.  
  47. //
  48. // Set the variable to something
  49. //
  50. $build->variable="There are a lot of e's in this sentence eh?";
  51.  
  52. //
  53. // Run the function from the object
  54. //
  55. $build->dosomething();
  56. ?>


That should print out:
Th3r3 ar3 a lot of 3's in this s3nt3nc3 3h?

As you can see, the function in the first class, will replace the e's with 3's. You may notice that we built the DiffClassName object, instead of the ClassName object to output the sentence. This shows us that you can use the extends method to output data obtained from the parent class. This is all for my tutorial.
RossT's Avatar
Author:
Views:
2,414
Rating:
There are currently no comments for this tutorial, login or register to leave one.