A group about Web Design & Development. Ask and Learn everything related to web design and development.

Report article RSS Feed Object-oriented programming - a practical guide

How to comprehend Object-Oriented Programming (OOP for short) in PHP, a practical example.

Posted by feillyne on Aug 1st, 2010
Basic Server Side Coding.

Object-oriented programming is a programming style that involves using objects through classes & methods (functions) to do one's work instead of functions and global/general variables.

As it is a practical guide, let's start with an example of a recruit presentation:

php code:
<?php

class Unit
{
  var $name = "Default Recruit";
  var $strength = 50;
 
    function recruitName()
      {
        echo "Recruit's name: " . $this -> name . "<br />";
        echo "Recruit's strength: " . $this -> strength . "<br /><br />";
      }
}

$newRecruit = new Unit;
$newRecruit -> recruitName();

$newRecruit -> name = "Julia";
$newRecruit -> strength = 55;
$newRecruit -> recruitName();


$secondRecruit = new Unit;
$secondRecruit -> name = "Scarlett";
$secondRecruit -> strength = 45;
$secondRecruit -> recruitName();

?>

First, you need to declare your class, in this case it is Unit class:

php code:
class Unit
{
  var $name = "Default Recruit";
  var $strength = 50;
}

See the variables have 'var' before them. This very 'var' actually means 'variable', and it must be placed before variables initialised in classes (unlike regular variables outside classes that don't have to have 'var' before them).

Next step, we'll create a function (method) for presenting our units / recruits called recruitName():

php code:
function recruitName()
{
  echo "Recruit's name: " . $this -> name . "<br />";
  echo "Recruit's strength: " . $this -> strength . "<br /><br />";
}

$this -> name
and
$this -> strength
are references to variables initialised earlier: variables name and strength.

"$this" simply means the current class / object, i.e. class Unit.
So
$this -> name
$this -> strength

could be translated as e.g.:
Unit.name
Unit.strength

(Unit object/class, name & strength variables/properties.)

So you should have by now this code, Unit class with recruitName method:

php code:
class Unit
{
  var $name = "Default Recruit";
  var $strength = 50;
 
    function recruitName()
      {
        echo "Recruit's name: " . $this -> name . "<br />";
        echo "Recruit's strength: " . $this -> strength . "<br /><br />";
      }
}

Now, we will create 2 objects.

php code:
$newRecruit = new Unit;
$newRecruit -> recruitName();

$newRecruit -> name = "Julia";
$newRecruit -> strength = 55;
$newRecruit -> recruitName();


$secondRecruit = new Unit;
$secondRecruit -> name = "Scarlett";
$secondRecruit -> strength = 45;
$secondRecruit -> recruitName();

As you can see, you can create objects thanks to new keyword. These two lines:
$newRecruit = new Unit;
$secondRecruit = new Unit;

create two objects contained in two variables: $newRecruit and $secondRecruit.

You can create another object, your own recruit, by typing in e.g.:
$myOwnRecruit -> new Unit;
$readersRecruit -> new Unit;

The rest of the code is using the in-class properties (variables) and methods (functions):
$newRecruit -> recruitName();

The first "$newRecruit -> recruitName();" calls the class Unit and the method recruitName().
So it's technically Unit class newRecruit example (object) call to its method recruitName().

Yes, objects are examples, instances of the given class (in this case, Unit class example, its "child").

When you create an object, you create a container / example of the given class.

After creating the new class and a call to the method recruitName:

php code:
$newRecruit = new Unit;
$newRecruit -> recruitName();

If you run the script file, you should get the first result (the first object/recruit) as below:
Recruit's name: Default Recruit
Recruit's strength: 50

Then we change the first object properties, first her name property to "Julia", then her strength property to 55 from 50. Finally, a call to show the recruit's name and strength on the screen (recruitName() function):

php code:
$newRecruit -> name = "Julia";
$newRecruit -> strength = 55;
$newRecruit -> recruitName();

Notice that we are still using $newRecruit variable.

If you check (run) the script, you'll see one object, which name & strength properties were changed:
Recruit's name: Default Recruit
Recruit's strength: 50

Recruit's name: Julia
Recruit's strength: 55

Then we proceed to create another "example", object of the class Unit:

php code:
$secondRecruit = new Unit;
$secondRecruit -> name = "Scarlett";
$secondRecruit -> strength = 45;
$secondRecruit -> recruitName();

Line:
$secondRecruit = new Unit;
creates an example of Unit class and assigns it to the $secondRecruit variable

Line:
$secondRecruit -> name = "Scarlett";
defines a new value for the name property of the object under the variable $secondRecruit

Line:
$secondRecruit -> strength = 45;
defines a new value for the strength property of the object under the variable $secondRecruit

Line:
$secondRecruit -> recruitName();
performs recruitName() function inside Unit class (two instructions that show recruit's name and strength)

So, the entire code should be now, as in the beginning:

php code:
<?php

class Unit
{
  var $name = "Default Recruit";
  var $strength = 50;
 
    function recruitName()
      {
        echo "Recruit's name: " . $this -> name . "<br />";
        echo "Recruit's strength: " . $this -> strength . "<br /><br />";
      }
}

$newRecruit = new Unit;
$newRecruit -> recruitName();

$newRecruit -> name = "Julia";
$newRecruit -> strength = 55;
$newRecruit -> recruitName();


$secondRecruit = new Unit;
$secondRecruit -> name = "Scarlett";
$secondRecruit -> strength = 45;
$secondRecruit -> recruitName();

?>

Perhaps you should experiment with these more.

Additional tasks:
1) create another class (e.g. "class Vehicle", or e.g. "class Plane")
2) define 4 properties/variables
3) create a couple of objects ("examples") of your own created class

Post comment Comments
SiPlus
SiPlus Oct 15 2010, 11:37am says:

Classes SUCK because they need too much code. Lua tables are the best.

+2 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Nov 13, 2007
Privacy
Public
Subscription
Open to all members
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Groups
Inside the Page
Inside the Page Hobbies & Interests group with 54 members
Web devs & web game devs
Web devs & web game devs Hobbies & Interests group with 35 members