Using classes and objects in php

PHP OOP — Classes and Objects

A class is a template for objects, and an object is an instance of class.

OOP Case

Let’s assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Define a Class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces (<>). All its properties and methods go inside the braces:

Syntax

Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>
?>

Note: In a class, variables are called properties and functions are called methods!

Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class are created using the new keyword.

Читайте также:  Python list create numbers

In the example below, $apple and $banana are instances of the class Fruit:

Example

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>

$apple = new Fruit();
$banana = new Fruit();
$apple->set_name(‘Apple’);
$banana->set_name(‘Banana’);

echo $apple->get_name();
echo «
«;
echo $banana->get_name();
?>

In the example below, we add two more methods to class Fruit, for setting and getting the $color property:

Example

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
function set_color($color) $this->color = $color;
>
function get_color() return $this->color;
>
>

$apple = new Fruit();
$apple->set_name(‘Apple’);
$apple->set_color(‘Red’);
echo «Name: » . $apple->get_name();
echo «
«;
echo «Color: » . $apple->get_color();
?>

PHP — The $this Keyword

The $this keyword refers to the current object, and is only available inside methods.

Look at the following example:

Example

So, where can we change the value of the $name property? There are two ways:

1. Inside the class (by adding a set_name() method and use $this):

Example

class Fruit public $name;
function set_name($name) $this->name = $name;
>
>
$apple = new Fruit();
$apple->set_name(«Apple»);

2. Outside the class (by directly changing the property value):

Example

class Fruit public $name;
>
$apple = new Fruit();
$apple->name = «Apple»;

PHP — instanceof

You can use the instanceof keyword to check if an object belongs to a specific class:

Источник

Object-Oriented PHP With Classes and Objects

Sajal Soni

Sajal Soni Last updated Dec 15, 2022

In this article, we’re going to explore the basics of object-oriented programming using PHP classes.

We’ll start with an introduction to classes and objects, and we’ll discuss a couple of advanced concepts like inheritance and polymorphism in the latter half of this article.

Читайте также:  100 php to audio

What Is Object-Oriented Programming (OOP)?

Object-oriented programming, commonly referred to as OOP, is an approach which helps you to develop complex applications in a way that’s easily maintainable and scalable over the long term. In the world of OOP (to create object in PHP), real-world entities such as Person , Car , or Animal are treated as objects. In object-oriented programming, you interact with your application by using objects. This contrasts with procedural programming, where you primarily interact with functions and global variables.

In OOP, there’s a concept of «class«, which is used to model or map a real-world entity to a template of data (properties) and functionality (methods). An «object» is an instance of a class, and you can create multiple instances of the same class. For example, there is a single Person class, but many person objects can be instances of this class— dan , zainab , hector , etc.

The class defines properties. For example, for the Person class, we might have name , age , and phoneNumber . Then each person object will have its own values for those properties.

You can also define methods in the class that allow you to manipulate the values of object properties and perform operations on objects. As an example, you could define a save method which saves the object information to a database.

Let’s keep diving in to learn how to create objects in PHP.

What Is a PHP Class?

First we need to understand what PHP classes are. A class is a template which represents a real-world entity, and it defines properties and methods of the entity. In this section, we’ll discuss the basic anatomy of a typical PHP class.

Читайте также:  Тег FIGCAPTION

The best way to understand new concepts is with an example. So let’s have a look at the Employee class in the following snippet, which represents the employee entity.

Источник

Оцените статью