⊗ppOpBsInr 1 of 107 menu

Introduction to OOP in PHP

Now we will study OOP in PHP. Let's consider a real-life example, and then transfer it to PHP.

Let's take a car as an example. It has wheels, color, body type, engine capacity, and so on. Besides, the driver can give it commands: go, stop, turn right, left, etc.

We can say that there is a certain class of cars that possesses common properties (all have wheels and all can be given commands).

A specific car standing on the street is a representative of this class, or, in other words, a object of this class. All objects of this class have properties: number of wheels, color, body type, and methods: go, stop, turn right, left.

In other words, the class itself is a blueprint according to which cars are made at the factory. The object is the car itself, made from these blueprints.

In PHP, a class is created using the keyword class, followed by the name of this class. Let's create a class Car:

<?php class Car { // here is the code, that is, the PHP drawing of the car } ?>

Let's now specify in our blueprint that any car created from this blueprint will have a property for color and a property for the amount of fuel.

To do this, inside the class, we write the property $color and the property $fuel:

<?php class Car { // Define properties (essentially class variables): public $color; // car color public $fuel; // amount of fuel } ?>

Let's now create the methods of our class. In PHP, methods, like regular functions, are declared using the keyword function, preceded by the keyword public.

As mentioned above, our car can go, can turn, can stop. Let's create corresponding methods in our class:

<?php class Car { public $color; // car color public $fuel; // amount of fuel // Command to go: public function go() { // some code } // Command to turn: public function turn() { // some code } // Command to stop: public function stop() { // some code } } ?>

We have made the blueprint of our car. Now we need to go to the factory and create an object of this class (i.e., a specific car).

In PHP, this is done using the keyword new, after which the class name is written:

<?php new Car; ?>

However, if you just create a class object - it will lead to nothing (it's the same as, for example, declaring an array and not storing it anywhere). We need a variable to store this object.

Let this variable be called $myCar - let's assign the object we created to it:

<?php $myCar = new Car; ?>

After creating the car, you can access its properties. Access to them happens via the arrow ->. Let's set the properties of our object:

<?php $myCar = new Car; // command the factory to make a car // Set the object's properties: $myCar->color = 'red'; // paint it red $myCar->fuel = 50; // fill it with fuel ?>

That's it, our car is created, painted, and refueled. Now we can give it commands through the methods of this car.

Access to methods also happens through the arrow, but, unlike a property, after the method name, you should write parentheses. Let's command our object:

<?php $myCar->go(); $myCar->turn(); $myCar->stop(); ?>
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline