PHP Classes and Objects

In this post, we will explore PHP classes and objects and examine how they can be created and used in the program.

We are going to cover a number of topics, including class properties, methods, as well as how to access class members with the help of the object operator.

Object-oriented programming refers to the concept that classes are blueprints for creating objects, and objects are instances of the classes that they belong to.

PHP is an object-oriented language, which allows developers to create classes and objects in order to be able to structure and modularize their code more effectively.



OOP Case

Let us suppose that we have a class named Student in our application.

There are many properties that can be assigned to a student, such as his name, his age, his gender, etc. In order to hold the values of such properties as name, age, and gender, we can define variables such as $name, $age, and $gender.

Each class inherits its properties and behaviors when it is created, but each object will have different values for the properties when they are created (for instance, std1, std2, etc will inherit properties and behaviors from the class).


PHP Classes – Create One

The class keyword is used when we want to define a class. Next, the name of the class is inserted, followed by a pair of curly braces ({}) to indicate a class.

Those braces contain all the properties and methods that belong to the class:

Syntax

<?php
class Student {
// code goes here…
}
?>

Listed below is a declaration of a class called Student with two properties ($name and $age) and two methods setName() and getName() to set and retrieve the values of $name respectively:

Example: 

<?php class Student { // Properties public $name; public $age;// Methods function setName($name) { $this->name = $name; } function getName() { return $this->name; } } ?>
Remember: Variables are referred to as properties and functions are referred to as methods within a class!

PHP Objects – Create One

Objects are the backbone of any class, and classes are nothing without them!

There are multiple objects that can be created from a single class.

The class defines all of the properties and methods that will be included in the object, but the values of those properties will be different from object to object.

With the new keyword, objects of a class can be created.

The following example shows $std1 and $std2 as instances of the Student class, respectively:

Example: 

<?php class Student { // Properties public $name; public $age; // Methods function setName($name) { $this->name = $name; } function getName() { return $this->name; } } $std1 = new Student(); $std2 = new Student(); $std1->setName('John'); $std2->setName('Alex'); echo $std1->getName(); echo "<br>"; echo $std2->getName(); ?>
We have added two new methods to the class Student, for setting and retrieving the $age property of the class Student, in the example below:

Example: 

<?php class Student { // Properties public $name; public $age; // Methods //set and get name function setName($name) { $this->name = $name; } function getName() { return $this->name; } //set and get age function setAge($age) { $this->color = $age; } function getAge() { return $this->color; } } $std1 = new Student(); $std1->setName('John'); $std1->setAge(22); echo "Name: " . $std1->getName(); echo "<br>"; echo "Age: " . $std1->getAge(); ?>

PHP $this Keyword

In a method, $this specifies which object is being referenced currently, and can only be used within that method.

Let’s take a look at an example of how this could be done:

Example: 

<?php class Student { public $name; } $std1 = new Student(); ?>

The question now is, where can the value of the $name property be changed? It can be done in two different ways:

1. Add the following to your class (by adding a setName() method to it and using $this):

Example: 

<?php class Student { public $name; function setName($name) { $this->name = $name; } } $std1 = new Student(); $std1->setName("John"); ?>
Example Explanation
In above example a class called Student with a single property called $name and a method called setName() that is used to set the value of the $name property.
When the code is executed, an object of the class “Student” is created using the new keyword and assigned to the variable $std1.
The setName() method is called on the object, passing in the value “John” as a parameter. This sets the value of the $name property of the object to “John”.
In object-oriented programming, a class is a blueprint for creating objects that have similar properties and behaviors. In this case, the “Student” class represents a student and has a property to store the student’s name and a method to set the name.
Once an object of the class is created, the properties and methods of the class can be accessed using the object variable. In this case, the setName() method is called on the $std1 object to set the name property to “John”.
2. Changing the property value directly outside the class (without changing in the class):

Example: 

<?php class Student { public $name; } $std1 = new Student(); $std1->name = "John"; ?>

PHP instanceof

If you want to know if an object belongs to a particular class, you can use the instanceof keyword:

Example: 

<?php class Student{ // code goes here } $std1 = new Student(); var_dump($std1 instanceof Student); ?>

Php Classes and Objects Benefits

Here are some benefits of using classes and objects in PHP:

  • Php Classes and objects allow you to reuse code that you have written for one project in another project. You can simply create an object of a class and use its methods in your code.
  • PHP Classes and objects allow you to encapsulate data and behavior within a single unit, which makes your code more organized and easier to understand.
  • By breaking your code into smaller, more manageable PHP classes and objects, you can make your code more modular and easier to maintain.
  • PHP Classes and objects allow you to abstract away the details of how something works and focus on what it does. This makes your code more flexible and adaptable to changing requirements.
  • Inheritance allows you to create a hierarchy of classes, where each class inherits the properties and methods of its parent class. This can help you create more specialized classes that are tailored to specific tasks.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *