PHP | Object Basics-I | Introduction to Object-Oriented Programming

This blog will cover the following topics in detail:

  • Classes and Objects

  • Constructor methods

Classes and Objects

Classes are often described in terms of Objects; Objects are often described in terms of Classes. This is interesting. To declare a class you need the keyword class and a class name. Class is the code template to generate one or more objects. Let's build a class.

class ShopProduct

{

// class body

}

Let's generate some objects. An Object is said to be an instance of its class.

$product1 = new ShopProduct();

$product2 = new ShopProduct();

Let's print them

var_dump($product1);

var_dump($product2);

Setting Properties in a Class

Classes can have special variables called properties. A property in a class looks similar to a standard variable but has a visibility keyword (public, protected, and private).

I'll explain the visibility keyword later. For now, let's use the Public keyword. You can access the property variables using the Object Operator (->) like this:

class ShopProduct

{

public $title = "default product";

public $producerMainName = "main name";

public $producerFirstName = "first name";

public $price = 0;

}

$product1 = new ShopProduct();

print $product1->title; // output: default product

As properties are public, you can assign value to them.

$product1 = new ShopProduct();

$product2 = new ShopProduct();

$product1->title = "Tom Cat";

$product2->title = "Jerry";

You can also add a new property dynamically to an Object like this:

$product1->randomProperty = "Dog"; // It's not a good practice in OOP

Let's set more properties on multiple objects.

$product1 = new ShopProduct();

$product1->title = " Tom Cat";

$product1->producerMainName = "Doe";

$product1->producerFirstName = "John";

$product1->price = "10";

print "author: {$product1->producerFirstName} " . " {$product1->producerFirstName}\n "; // Output: John Doe

What if I mistakenly type $product1->producerSecondName = "Doe";

As far as the PHP engine is concerned it is a legal expression. It won't throw any errors. But when I want to print it I would get unexpected results. This problem can be solved by introducing methods.

Working with Methods

Just as properties allow your objects to store data, methods allow your objects to perform tasks. Methods are special functions declared within a class.

public function classMethod ($arg1, $arg2,...)

{

// method body

}

The method can be public, private, or protected.

class ShopProduct

{

public $title = "default product";

public $producerMainName = "main name";

public $producerFirstName = "first name";

public $price = 0;

public function getProducer()

{

return $this->producerFirstName . " " . $this->producerMainName;

}

}

$product1 = new ShopProduct();

$product1->title = "My Product";

$product1->producerMainName = "Doe";

$product1->producerFirstName = "John";

$product1->price = 8.07;

print "author: {$product1->getProducer()}\n "; // Output: John Doe

Creating a Constructor Method

A constructor method is invoked when an object is created. You can use it to set up, ensuring that properties are assigned values and any necessary preliminary work is done.

class ShopProduct

{

public $title;

public $producerMainName;

public $producerFirstName;

public $price = 0;

public function __construct(

$title,

$firstName,

$mainName,

$price

) {

$product1->title = $title;

$product1->producerMainName = $mainName;

$product1->producerFirstName = $firstName;

$product1->price =$price;

}

public function getProducer()

{

return $this->producerFirstName . " " . $this->producerMainName;

}

}

$product1 = new ShopProduct("My Product", "John", "Doe",8.07);

print "author: {$product1->getProducer()}\n "; // Output: John Doe

A ShopProduct object is easier to instantiate and safe to use.