Page 1 of 1

OOP in PHP One - Classes and Inheritance A guide to OOP in PHP Rate Topic: ***** 2 Votes

#1 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Posted 26 July 2008 - 03:51 PM

Before we start, this tutorial is meant for those who are at an intermediate level of PHP.

OOP stands for Object-Oriented Programming. It is a programming concept that caught on in the 1990's. OOP focuses on 'objects' which are, well, objects. They have certain characteristics, and can behave in certain ways. OOP programming has a few concepts that define it. One of the defining features we will start with is called a class.

A class shows what an object has and can do, and it consists of members. Members can be divided into properties and methods. Properties are the characteristics of the object. For example, cheese (object) has the properties of type (maybe Gorgonzola, or cheddar), color (green, or white), and flavor (awful or delicious). Methods are the actions and behaviors the object can do. For example, cheese (object) can mold. Now let's see the technical side of it.

class Cheese { // A class, shows what all cheese has
	var $type; // These are a class's attributes, or properties
	var $flavor; // They are sometimes called characteristics, too.
	var $color; // All cheeses have these 3 properties
	
	// These functions are called 'methods'
	// It's what the cheese can do for you
	// and what you can do for your cheese
	function giveDetails ($thetype, $theflavor, $thecolor) {
		$this->type = $thetype;
		$this->flavor = $theflavor;
		$this->color = $thecolor;
	}
	
	function showType() {
		return $this->type;
	}
	function showColor() {
		return $this->color;
	}
	function showFlavor() {
		return $this->flavor;
	}
}


You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.

Now let's see this class in action.

<?php

class Cheese { // A class, shows what all cheese has
	var $type; // These are a class's attributes, or properties
	var $flavor; // They are sometimes called characteristics, too.
	var $color; // All cheeses have these 3 properties
	
	// These functions are called 'methods'
	// It's what the cheese can do for you
	// and what you can do for your cheese
	function giveDetails ($thetype, $theflavor, $thecolor) {
		$this->type = $thetype;
		$this->flavor = $theflavor;
		$this->color = $thecolor;
	}
	
	function showType() {
		return $this->type;
	}
	function showColor() {
		return $this->color;
	}
	function showFlavor() {
		return $this->flavor;
	}
}

$zargento = new Cheese; // Zargento is a brand of cheese

// We will now give it characteristics
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");

// Now let's see those details
echo $zargento->showType();
echo "<br>"; // It seems DIC likes to get rid of my HTML br tags
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();

?>


You make a new object by using 'new'. $zargento is now a Cheese object. It has access to all the properties and methods we outlined in the class. If we want $zargento to use the giveDetails() function, you use '$zargento->giveDetails()' as was used above. If you run the above script, the output will be:

Gorgonzola
Awful
Green and white


Now that you've gotten a good idea of how classes work, we can move one step further with another concept of OOP called inheritance. This allows you to create new classes using already created classes. Here is an example:

class MoreCheese extends Cheese {
	var $cost;
	
	function giveCost($f) {
		$this->cost = $f;
	}
	
	function showCost() {
		return $this->cost;
	}
}


You use 'extends' to grab the methods and properties from the Cheese class and add them to the MoreCheese class. Let's see the full code.

<?php

class Cheese { // A class, shows what all cheese has
	var $type; // These are a class's attributes, or properties
	var $flavor; // They are sometimes called characteristics, too.
	var $color; // All cheeses have these 3 properties
	
	// These functions are called 'methods'
	// It's what the cheese can do for you
	// and what you can do for your cheese
	function giveDetails ($thetype, $theflavor, $thecolor) {
		$this->type = $thetype;
		$this->flavor = $theflavor;
		$this->color = $thecolor;
	}
	
	function showType() {
		return $this->type;
	}
	function showColor() {
		return $this->color;
	}
	function showFlavor() {
		return $this->flavor;
	}
}

class MoreCheese extends Cheese {
	var $cost;
	
	function giveCost($f) {
		$this->cost = $f;
	}
	
	function showCost() {
		return $this->cost;
	}
}

$zargento = new MoreCheese;
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>


As you can see, even though $zargento is no longer Cheese, and is now MoreCheese, it still retains all of the methods and properties from Cheese because the MoreCheese class inherits all of them from Cheese. The advantages to inheritance are that you don't have to edit the base class (in this case Cheese). You can also override code, as this example shows.

<?php

class Cheese { // A class, shows what all cheese has
	var $type; // These are a class's attributes, or properties
	var $flavor; // They are sometimes called characteristics, too.
	var $color; // All cheeses have these 3 properties
	
	// These functions are called 'methods'
	// It's what the cheese can do for you
	// and what you can do for your cheese
	function giveDetails ($thetype, $theflavor, $thecolor) {
		$this->type = $thetype;
		$this->flavor = $theflavor;
		$this->color = $thecolor;
	}
	
	function showType() {
		return $this->type;
	}
	function showColor() {
		return $this->color;
	}
	function showFlavor() {
		return $this->flavor;
	}
}

class MoreCheese extends Cheese {
	var $cost;
	
	function giveDetails($q) {
		echo $q;
	}
	
	function giveCost($f) {
		$this->cost = $f;
	}
	
	function showCost() {
		return $this->cost;
	}
}

$zargento = new MoreCheese;
$zargento->giveDetails("dilly");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>


As you can see, the giveDetails() function of the base class Cheese has been overrided by the giveDetails() function of the MoreCheese class. This is a very useful feature of inheritance.

Thank you for reading this part of the tutorial, and I hope you learned something :D
~Moonbat

Is This A Good Question/Topic? 4
  • +

Replies To: OOP in PHP One - Classes and Inheritance

#2 brandon99337  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 167
  • Joined: 14-February 08

Posted 20 August 2008 - 01:50 PM

Thanks so much for this tutorial, it's exactly what I needed to make the lightbulb go off!
Was This Post Helpful? 0
  • +
  • -

#3 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Posted 26 November 2008 - 01:44 PM

Thanks, I'll probably be using this in the near future :).
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Posted 10 May 2009 - 05:21 PM

Thank you very much for this, this helped me understand a LOT better.
Maybe all PHP features should relate to food ;)

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#5 CoderManNET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 09

Posted 19 May 2009 - 03:39 AM

Hmm... very good tutorial, good work dude :)
Was This Post Helpful? 0
  • +
  • -

#6 clickfanatic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-January 09

Posted 26 August 2009 - 07:52 AM

finally, a tutorial thats very easy to understand. thanks a lot hope to get more oop tutorials from you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1