Create object with 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.

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:

Источник

PHP Objects

Summary: in this tutorial, you will learn about PHP objects, how to define a class, and how to create an object from a class.

What is an Object

If you look at the world around you, you’ll find many examples of tangible objects: lamps, phones, computers, and cars. Also, you can find intangible objects such as bank accounts and transactions.

All of these objects share the two common key characteristics:

For example, a bank account has the state that consists of:

A bank account also has the following behaviors:

PHP objects are conceptually similar to real-world objects because they consist of state and behavior.

An object holds its state in variables that are often referred to as properties. An object also exposes its behavior via functions which are known as methods.

What is a class?

In the real world, you can find many same kinds of objects. For example, a bank has many bank accounts. All of them have account numbers and balances.

These bank accounts are created from the same blueprint. In object-oriented terms, we say that an individual bank account is an instance of a Bank Account class.

By definition, a class is the blueprint of objects. For example, from the Bank Account class, you can create many bank account objects.

The following illustrates the relationship between the BankAccount class and its objects. From the BankAccount class you can create many BankAccount objects. And each object has its own account number and balance.

Define a class

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

 class ClassName < //. >Code language: HTML, XML (xml)

For example, the following defines a new class called BankAccount :

 class BankAccount  Code language: HTML, XML (xml)

By convention, you should follow these rules when defining a class:

  • A class name should be in the upper camel case where each word is capitalized. For example, BankAccount , Customer , Transaction , and DebitNote .
  • If a class name is a noun, it should be in the singular noun.
  • Define each class in a separate PHP file.

From the BankAccount class, you can create a new bank account object by using the new keyword like this:

 class BankAccount  < >$account = new BankAccount();Code language: HTML, XML (xml)

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

$account = new BankAccount;Code language: PHP (php)

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 BankAccount class is empty because it doesn’t have any state and behavior.

Add properties to a class

To add properties to the BankAccount class, you place variables inside it. For example:

 class BankAccount < public $accountNumber; public $balance; >Code language: HTML, XML (xml)

The BankAccount class has two properties $accountNumber and $balance . 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:

 $object->property;Code language: HTML, XML (xml)

The following example shows how to set the values of the accountNumber and balance properties:

 class BankAccount < public $accountNumber; public $balance; > $account = new BankAccount(); $account->accountNumber = 1; $account->balance = 100;Code language: HTML, XML (xml)

Besides the public keyword, PHP also has private and protected keywords which you’ll learn in the access modifiers tutorial.

Add methods to a class

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

 class ClassName < public function methodName(parameter_list) < // implementation > >Code language: HTML, XML (xml)

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 BankAccount class:

 class BankAccount < public $accountNumber; public $balance; public function deposit($amount) < if ($amount > 0) < $this->balance += $amount; > > >Code language: HTML, XML (xml)

The deposit() method accepts an argument $amount . It checks if the $amount is greater than zero before adding it to the balance.

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

$object->method(arguments)Code language: PHP (php)

The new syntax in the deposit() method is the $this variable. The $this variable is the current object of the BankAccount class.

For example, when you create a new object $account and call the deposit() method, the $this inside the method is the $account object:

$account = new BankAccount(); $account->accountNumber = 1; $account->balance = 100; $account->deposit(100);Code language: PHP (php)

Similarly, you can add the withdraw() method to the BankAccount class as follows:

 class BankAccount < public $accountNumber; public $balance; public function deposit($amount) < if ($amount > 0) < $this->balance += $amount; > > public function withdraw($amount) < if ($amount $this->balance) < $this->balance -= $amount; return true; > return false; > >Code language: HTML, XML (xml)

The withdraw() method checks the current balance.

If the balance is less than the withdrawal amount, the withdraw() method returns false .

Later, you’ll learn how to throw an exception instead. Otherwise, it deducts the withdrawal amount from the balance and returns true .

Summary

  • Objects have states and behaviors.
  • A class is a blueprint for creating objects.
  • Properties represent the object’s state, and methods represent the object’s behavior. Properties and methods have visibility.
  • Use the new keyword to create an object from a class.
  • The $this variable references the current object of the class.

Источник

Читайте также:  Php cookies сохранить массив
Оцените статью