posted by John Collins on Wed 21st Apr 2004 06:42 UTC

"OOP for beginners, Page 3/3"

Now, any function can accept parameters, you just add that to the class, and then when you call the functions, you pass the values in. Let's say we were to rewrite our "rideTheBus()" function to the following:

PHP Code:

 ... 
   function rideTheBus($busNumber) { 
      $statement = $this->Name . " rides bus number " . $busNumber; 
      return $statement; 
   } 
 ... 

then in your main program code:

PHP Code:

.. 
   print $a_student->rideTheBus("65"); 
..  

So what did we do? well in the rideTheBus() function, we told it to accept a value, and place it into the variable $busNumber. We then created a statement that says the student's name, followed by the words "rides bus number" and then the value placed in $busNumber. The return statement says that when that function is called, return the value in $statement to whatever called the function. We then passed bus number "65" into the function. The result of the above code would be to print out the following:

John rides bus number 65.

Now, there are other ways of printing the results of the function. In our main code, we said "print the value RETURNed by the function rideTheBus(). We could have done this:

PHP Code:

.. 
   $busRideStatement = $a_student->rideTheBus("65"); 
   print $busRideStatement; 
..  

This code does the same thing, but first dumps the sentence into the $busRideStatement variable, so we could reuse it later by just calling the variable, rather than having to recall the function. The complete code of what we did is below:

File: Student.php
PHP Code:

<?php 
class Student { 

   /* These are some of the properties */ 
   var $Name; 
   var $Gender; 
   var $GradeLevel; 
   var $Schedule; 

   /* Constructor. Called as soon as we create a student */ 
   function Student($studentName, $studentGender) { 
      $this->Name = $studentName; 
      $this->Gender = $studentGender; 
   } 

   function goToClass() { 
   } 

   function rideTheBus($busNumber) { 
      $statement = $this->Name . " rides bus number " . $busNumber; 
      return $statement; 
   } 

   function skipSchool() { 
   } 

   function changeGender() { 
      if ($this->Gender == "Female") { 
         $this->Gender = "Male"; 
      } else { 
         $this->Gender = "Female"; 
      } 
   } 
}
?> 

File: index.php
PHP Code:

<?php 
include "Student.php"; 

$a_student = new Student("John","Male"); 
$b_student = new Student("Mary","Female"); 
$c_student = new Student("Larry","Female"); 

print $a_student->Gender; 
print "<br>"; 
print $b_student->Gender; 
print "<br>"; 
print $c_student->Gender . " " . $c_student->Name; 

print $a_student->rideTheBus("65"); 

$c_student->changeGender(); 
print $c_student->Gender; // this will print "Male" now... 

?> 

That is the basics of OOP in a nutshell. The terms you should be familiar with include: parameters, functions, class, constructor, variable scope, return statements, instantiation, initialization.

Again allow me to reiterate that this article/tutorial is by no means comprehensive. Some advanced topics include: passing by reference, destructors - (just a special function, called when you destroy your object), polymorphism, inheritance, and default values. I am also quite sure there are other terms I'm not remembering right now, but the basics of how OOP works is all there.

I hope that for the novice programmer interested in learning the concepts of OOP, I've generally explained some of the basics. Remember that if you're programming in a language other than PHP, the core concepts of OOP are still the same! You sill create reusable chunks of code, pass values to functions, and access properties of your objects. I'd like to encourage you to try writing a sample class of your own, and creating your own objects. It is your code, you can do whatever you want! I created a female student named Larry and was able to quickly give him a sex change...try doing that in real life! For those interested in using the php example I've created above, but don't know how to get started using php, I highly recommend heading over to www.php.net . Windows users may also wish to take a look at www.firepages.com.au , where a very nice man has taken the time to bundle everything in an easy-to-use zip file, so you can get started coding quickly, without the hassle of setting up PHP. On that note, I shall throw away my now empty bottle of Mt. Dew, close trusty winamp, and bid you, the reader, adieu.

About the Author:
John Collins has been writing code for almost 20 years, since he was 5 years old writing in BASIC on his Commodore64. He enjoys coding, sports, and reading. He currently serves as lead developer, webmaster, and computer technician for a school division in Virginia, USA.
If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
Table of contents
  1. "OOP for beginners, Page 1/3"
  2. "OOP for beginners, Page 2/3"
  3. "OOP for beginners, Page 3/3"
e p (0)    59 Comment(s)

Technology White Papers

See More