PHP Class Tutorial: A Beginning to OOP

When we talk about OOP (Object Oriented Programming), the name of classes, objects, encapsulation, polymorphism and more comes in mind. Most of us think, OOP is a hard and difficult to understand concept – but believe me it is more interesting, easy to learn and loving concept. The aim of this tutorial is to clarify the basic concept of PHP Classes. You will be master of following concepts after reading this tutorial,

  1. Keyword: class
  2. Valid Class Name
  3. Class Syntax
  4. Concept of Class and Objects
  5. Operator: new
  6. Class Instance

In short, a class is a code template used to generate objects. You declare a class with the class keyword and an arbitrary class name. Class names can be any combination of numbers and letters, although they must not begin with a number. The code associated with a class must be enclosed within braces. Let’s combine these elements to build a class.

/*
|------------------
| Class Tutorial
|------------------
*/

class Product {
	// Class Body
}

The Product class in the example is already a legal class, although it is not terribly useful yet. The power of this should become clearer as we move together with the series of oop tutorials. We have successfully learned the basic idea of class declaration.

A First Object

I am going to share, two very important concepts of OOP. So please read them twice. These concepts in the short lines will be the concrete base of our OOP programming.

A class is a template for generating objects

Whereas,

An object is data that has been structured according to the template defined in a class. An object is said to be an instance of its class. It is of the type defined by the class

I use the Product class as a mold for generating Product objects. To do this, I need the new operator. The new operator is used in conjunction with the name of a class, like this:

/*
|------------------
| Class Instance
| Generate Objects
|------------------
*/

$product1 = new Product();
$product2 = new Product();
  • The new operator is invoked with a class name as its only operand and generates an instance of that class; in our example, it generates a Product object.
  • I have used the Product class as a template to generate two Product objects. Although they are functionally identical (that is, empty), $product1 and $product2 are different objects of the same type generated from a single class.

Little Explanation

If you are still confused, try this analogy. Think of a class as a cast in a machine that makes plastic ducks. Our objects are the ducks that this machine generates. The type of thing generated is determined by the mold from which it is pressed.

The ducks look identical in every way, but they are distinct entities. In other words, they are different instances of the same type. The ducks may even have their own serial numbers to prove their identities. Every object that is created in a PHP script is also given its own unique identifier (unique for the life of the object), that is, PHP reuses identifiers, even within a process. I can demonstrate this by printing out the $product1 and $product2 objects:

/*
|------------------
| Printing Object 1 and Object 2
|------------------
*/

echo "<pre>";
var_dump($product1);
var_dump($product2);
echo "</pre>";

/*
|------------------
| Output
|------------------
*/

object(Product)#1 (0) {
}
object(Product)#2 (0) {
}

By passing our objects to var_dump(), We come to know very useful information including, after the hash sign, each object’s internal identifier. I hope you have learned basic concept of class.

Complete Tutorial: PHP Class

/*
|------------------
| Class Tutorial
|------------------
*/

class Product {
	// Class Body
}

/*
|------------------
| Class Instance
| Generate Objects
|------------------
*/

$product1 = new Product();
$product2 = new Product();

/*
|------------------
| Printing Object 1 and Object 2
|------------------
*/

echo "<pre>";
var_dump($product1);
var_dump($product2);
echo "</pre>";

Stay in touch by subscribing your email to the quality contents, products and tutorials of TutorialChip – and don’t forget to share your comments.

3 thoughts

Comments are closed.