PHP Objects and Classes

Objects and classes are used to structure all of the code in the object-oriented programming (OOP) paradigm. Building reliable, scalable, and maintainable online applications with PHP requires a fundamental understanding of objects and classes. In this article, we will learn about PHP Objects and Classes with the help of examples.

Class

According to OOPs concept, a class is the blueprint / template of an object. It contains the similar types of objects having the same states (properties) and behavior.

In other words, A class a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use.

For example,

Let us consider two objects Samsung Galaxy S4 and iPhone. Suppose Samsung Galaxy S4 have some properties like width = “6.98 cms”, height = “13.6 cm”, OS = “Android”, brand = “Samsung”, price = “1000$” and actions are call(), sendMessage(), browser(), share().

Now, suppose iPhone has some properties such as width = “5.86 cm”, height = “12.3 cms”, OS = “iOS”, brand = “Apple”, price = “1200$” and actions are call(), sendMessage(), browse(), share().

Both are objects & have the similar properties and actions but the type is the same “Phone”. This is the class. i.e the name of the class is “Phone”.

Define a class

To define a class, you specify the class keyword followed by a name like this:

<?php

class ClassName
{
    //...
}
For example,

the following defines a new class called MobilePhone:

<?php

class MobilePhone
{
	
}

When we define a class, we should follow some rules:

  • A class name should be in the upper camel case where each word is capitalized. For example, MobilePhone, Person, Rectangle etc.
  • If a class name is a noun, it should be in the singular noun.
  • Define each class in a separate PHP file.

From the MobilePhone class, you can create a new mobilephone object by using the new keyword like this:

<?php

class MobilePhone
{
}

$mobnew = new MobilePhone();

In this syntax, the $mobnew is a variable that references the object created by the MobilePhone class. The parentheses that follow the MobilePhone class name are optional. Therefore, you can create a new MobilePhone object like this:

$mobnew = new MobilePhone;

The process of creating a new object is also called instantiation. In other words, you instantiate an object from a class. Or you create a new object from a class. The MobilePhone class is empty because it doesn’t have any state and behavior.

Also read about PHP Database Connection

Add properties to a class

To add properties to the MobilePhone class, you place variables inside it.

For example,
<?php

class MobilePhone
{
    public $simcard;
    public $internalmemory;
}

The MobilePhone class has two properties $simcard and $internalmemory. In front of each property, you see the public keyword. The public keyword determines the visibility of a property. In this case, you can access the property from the outside of the class.

To access a property, you use the object operator (->) like this:

<?php

$object->property;
?>

The following example shows how to set the values of the simcard and internalmemory properties:

<?php

class MobilePhone
{
    public $simCard;
    public $internalMemory;
}

$mobnew = new MobilePhone();

$mobnew->simcard = 'Airtel';
$mobnew->internalMemory = '8GB';

?>

Besides the public keyword, PHP also has private and protected keywords which we will learn coming blogs.

Add methods to a class

The following shows the syntax for defining a method in a class:

<?php

class ClassName
{
	public function methodName(parameter_list)
	{
		// implementation
	}
}

?>

Like a property, a method also has one of the three visibility modifiers: public, private, and protected. If you define a method without any visibility modifier, it defaults to public.

The following example defines the deposit() method for the MobilePhone class:

<?php

class MobilePhone
{
	public $simCard;

	public $internalMemory;

	public function dialNumber($simCard)
	{
		$this->simCard = 'Airtel';
	}
}

?>

The dialNumber() method accepts an argument $simCard. It checks default sim for dial mobile number.

To call a method, you also use the object operator (->) as follows:

$object->method(arguments)

The new operator dynamically allocates memory for an object returns a reference to object and binds the reference with the object. Here reference to object means address of object.

To write object-oriented PHP code effectively, one must comprehend objects and classes. It is possible for developers to construct clear, modular, and reusable code by establishing classes with attributes and functions and producing objects. PHP is a strong tool for creating intricate web applications since it makes use of visibility and access modifiers, which improve the code’s encapsulation and security.

Categorized in: