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

"OOP for beginners, Page 2/3"

I'll get to the example in a second. I also need to mention, that functions can take "arguments" or "parameters." These are values that are passed into the function. The function assigns these values to variables, which it can then use and/or manipulate. The values are passed in via the parenthesis of the function. Back to our example:

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 (aka "instantiate the object") */ 
   function Student($studentName, $studentGender) { 
        $this->Name = $studentName; 
        $this->Gender = $studentGender; 
   } 

   function goToClass() { 
   } 

   function rideTheBus() { 
   } 

   function skipSchool() { 
   } 
}
?>

What did we do? Well we added a constructor, and we told it to accept two parameters. These parameters are assigned the variable names of "$studentName" and "$studentGender." Now, these two variables can only be used INSIDE THIS FUNCTION. That's called "variable scope". The next two lines of code say "I want to take the values passed into this function, and assign them to the object." What does that mean? Well it means that when you create the object now, you can access $Name, and $Gender. More examples...

PHP Code:

<?php 
   include "Student.php";
   $a_student = new Student("John","Male"); 
?>

I kept it simple. The code above would be a new file, while we save the rest of our example in "Student.php" The first line just makes sure that our template (the class file) is accessible. The second line is where the magic happens. It creates a new instance of the Student object, and passes two values to it - "John" and "Male". Recall that when we create an object, the object's "constructor" function is called immediately. Our constructor wants two values, the student name and the gender. For that reason, we passed these values in as we created the object.

You create instances of objects in PHP using the "new" keyword. The syntax is as follows:

Code:

variable = new ObjectName(parameters)

So now we have a real-code, digital student. How exciting. What do we do with him? Torture? Detention? Hmm....these all sound good, but we'll start simply, but displaying some information about him.

PHP Code:

<?php 
   include "Student.php"; 

   $a_student = new Student("John","Male"); 
   print $a_student->Name; 
?>

Everytime you access something in an object you create (in PHP), you use the "->" characters. Some programming languages use periods. It depends on the language. (As an example, if you created that same object in JAVA, you'd print the "$Name" property by typing:

Code:

   System.out.print(a_student.Name);

(In Java, you use "System.out.print" instead of just "print", like in php).

So we have now have a Student object. How do we make him do the things we created in our template? (ie the functions). Well once you've added some code to the functions, you can make the object perform those function by doing the following:

PHP Code:

<?php 
   include "Student.php"; 
   
   $a_student = new Student("John","Male"); 
   print $a_student->Name; 

   $a_student->goToClass(); 
?>

That last line tells the Student object to call the function goToClass(). Poor John. I'm certain he doesn't want to go class. Now where's the benefit of the function? Recall that OOP gives us access to reusable chunks of code. What if you wanted to create lots of students? An example:

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; 

?>

That code creates 3 students. It then prints out the first two student's genders. It then prints the third student's Gender and his...er....her name. (I blame that on Winamp...Culture Club just started playing.....) Are you starting to see some of the benefits? You could then make b_student goToClass, and maybe have c_student skipSchool. These are spiffy things your program can do. Of course, you could write a "changeGender()" function...which seems to be particularly appropriate in the case of Larry.... It might look something like this (remember that this is a "Student" function, so this code goes inside the class. Also note that when I put '...' before and after code, it means code exists before and after, but I'm not going to retype it all. Just stick this function somewhere inside your class, maybe after the skipSchool() function.)

PHP Code:

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

Hehe. Place that code in the class, and whenever you want to perform a sex change in your main code, just do something similar to the following:

PHP Code:

.. 
   $a_student->changeGender(); 
..  

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