posted by John Collins on Wed 21st Apr 2004 06:42 UTC
IconThe purpose of this article is to give a novice programmer the basic idea of what OOP is, as implemented using PHP. Readers should have a basic knowledge of programming ie what variables are, variable types, basic methods of writing comments, and how to enter code into a text editor.

Also, other programming languages will most certainly use different keywords and methods of doing things (like defining a class, labeling a variable, etc). Remember to look for the concepts in this article, and not to get caught up with semantical details.

To begin, it is important to stress that one of the core ideas behind OOP is that code written by the programmer is placed in reusable chunks. Reusable chunks of code save the developer time and money during the coding process, as well as making the code much more efficient. These reusable chunks of code are then used to create what we call an object; the object can be manipulated, made to do things, etc.

Before you actually "create an object," you design the template for the object. This template is called a "class." All code that the object does, and properties of the object are placed inside the class. As mentioned previously, I'll be using PHP for demonstration purposes, as it is one of the quicker and easier coding languages to learn. Below is some sample code, which simply creates a class called "Student":

PHP Code:

<?php 
class Student { 

}
?>

The name of our sample class is "Student." Classes are recognized in php by the "class" keyword. What we want to do is create the template for a typical "student." The student can do things. Some of the things the student can do include: go to class, skip school, go to lunch, turn in homework, ride the cheesewagon (er bus), that sort of thing. Anything we want the typical student to be able to do, we put inside what is called a "function" (some languages call these "methods", ie Java).Continuing with our example:

PHP Code:

<?php 
class Student { 

   function goToClass() { 
   } 

   function rideTheBus() { 
   } 

   function skipSchool() { 
   } 
} 
?> 

Ok great. Now our template has some basic actions that our student can perform. We don't actually have the code inside of our functions, but we've created the functions.

Another thing that we want to give objects are properties. Every property we want assigned to the student is also placed inside the class. Properties might include the student's name, their grade level, their gender, their schedule, etc. On to the example:

PHP Code:

<?php 
class Student { 

   var $Name;
   var $Gender; 
   var $GradeLevel; 
   var $Schedule; 

   function goToClass() { 
   } 

   function rideTheBus() { 
   } 

   function skipSchool() { 
   } 
} 
?> 

Before continuing, I want to stress the importance of two definitions: instantiation and initialization.

Instantiation is when you actually create an object in your code. Remember that classes are templates, or definitions for objects, very similar to blueprints. Blueprints aren't the actual house, but the design for the house. Instantiation is when you actually build the house in your code (or in this case, you create your student).

Initialization is when you first assign a value to a variable. I mention this definition here because novice programmers can easily be confused by the differences in the two words...it certainly doesn't help that they sound very much alike!

Ok, back to the code. We've created some variables in our template. Unfortunately, nothing is actually assigned to the Student object; we just have empty variables. (these variables have not been initialized). You can assign values to variables in your class using the "this" keyword. Also, if you have a function called the same thing as the class name ("Student"), then when you create an "instance" of the object in your code (ie, you create a student in your application), then that function is called automatically, as soon as you create the instance of the object. This function has a special name, called a "constructor".

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