Object Oriented Programming

Creator:
Eric
Description:
The basics of object oriented programming. Using classes, functions, instances, etc.

Introduction

PHP is a very powerful tool. The question how will you wield it? With standard programming or with the quick object-oriented programming? Well, the choice is up to you. I find standard a bit easier and quicker to write, but I find the speed and organization of object oriented programming make it no comparison. In this guide I will teach you the basics, and pretty much as much as I know, about Object Oriented Programming (here-on-out known as OOP).

Terms to know:

Class-This is basically what stores local variables and functions, to be later acted upon with an object.
Object Instance-A use of a class, somewhat confusing at first, but I'll go more into later.
Member Variable-A variable inside the class.
Member Function-A function inside the class.
Child Class-A child class extends a parent class, holding all of it's member variables and functions. It can then have additions put on it.
Constructor-A function that takes the class vars (from the initiation) and assigns them variables.

Syntax

Class & Member Variables and functions:

class className { var $myvar; function greet(){ print "Hello World"; }}

Not very much to look at. The top part is really just declaring the class, and the $myvar part is declaring a member variable, very simple. Declaring a function is very easy, just like regular. There is some more to it that I will talk about later in the tutorial.

Object Instance:

$newClass=new className; $newClass->greet();

Again very simple once understood, it took me a little while to catch on due to bad explanations, but anyhow: The $newClass=new className is just creating a new instance of the class I made up above. What is that doing really? It's just allowing you to access the member variables and classes using the variable you declared it as. Then as you can see on the next line, it's all still very simple, you use the variable that you declared the object instance as, and an arrow (->) then write what you want. Variables do not have to have another dollar sign.

Child Class:

class newchild extends className{ }

Very simple, you just add an extends and the name of the class that you want it to be a child of. A class can only have one parent. I'm not going into much depth here, as I don't feel it's really necessary. If you have questions feel free to ask.

Functions:
class example { var $count=0; function writeb($text){ print '<b>'.$text.'</b>'; $this->count++; }}

All I really want to point out here is the $this->count++ part, as you can see it is almost exactly like a new object instance, only difference is it uses $this. In functions, to use member variables you have to put an $this-> in front, very simple.

Constructor:

class consex { var $count; var $rem; function consex($count,$rem){ $this->count=$count; $this->rem=$rem; }

You put all the vars to register in the function declaration, then you can put everything else in there. A constructor is called every time an instance of the class is called. An example of calling a class with a constructor: $cons=new consex(3, "action");

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <b> <u> <i> <hr> <img src <url=
  • Lines and paragraphs break automatically.

More information about formatting options