• Register

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

Post tutorial Report RSS Object-oriented programming - a practical guide

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

Posted by on - 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

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:

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():

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:

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.

$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:

$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):

$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:

$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

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 - - 290 comments

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

Reply Good karma Bad karma+2 votes
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: