PHP Object Oriented Programming Tutorial Part 1

Before learning Object Oriented Programming in PHP, it’s a good idea to learn basic PHP first so that it’s easier to learn later. In this tutorial, we will learn about Introduction to Object Oriented Programming, learn classes, inheritance, polymorphism, interfaces, traits, errors and others.

What is Object Oriented Programming ?

Object Oriented Programming or OOP is a method or point of view of a programming language whose orientation or concept is called an object, so the main focus on Object Oriented Programming is the object itself, there are many methods or points of view in programming languages such as Functional Programming, Object Oriented Programming and procedural, but the most famous and widely used by programmers is Object Oriented Programming methods .

In Object Oriented Programming there are 2 important terms that is Object and Class and then they are developed again.

What are Objects ?

Object is data which contains fields or properties or attributes and methods or functions or behavior.

What are Classes ?

Class is blueprint or prototype for creating an object, so we cannot create an object without a class. Class contains declarations of all the properties and functions of an object, from a class we can create unlimited or as many objects as possible.

Create Classes

In PHP, to create a class, we can use the keyword “class” and for class names, we can use the CamelCase formats (no spaces and must begin with an uppercase letter).

To make the class name not have to be the same as the file name, just to make it easier for us later in managing the class, try to make the class name the same as the file name.

We will try to make a php program to create classes, here I assume you have installed xampp for the local web server, if not you can check the tutorial here.

Ok, let’s just practice how to create classes and objects in php, please create a new folder in htdocs with the name “learning_opp”, in the learning_opp folder create a new folder with the name “data”, then open Vscode and open the learning_opp folder and created a new file in the data folder with the name “Mobil.php” inside Mobil.php type the following code

<?php
class Mobil
{
    // take the function, properties for object here
}

?>

after we created the class, now we will create the object, create a new file in the learning_opp folder with the name “object.php” and fill in the following code

<?php

require_once "data/Mobil.php"; //include file Mobil.php in the data folder 

$mobil  =   new mobil(); //create new object that called mobil

var_dump($mobil);

?>

then in Vscode run the object.php code by selecting the top navigation menu run > run without debugging > Run current script in console, if successful it will look like the image below

Create a class and create an object
Create a class and create an object then run it in Vscode – photo by webbaliseo

 

What is the Properties / Fields / Attributes ?

The Properties / Fields / Attributes are the data that we can insert into the object, but before we can insert the data of properties into the object, we must declare what the data of object has in its class declaration, to make the properties of the object that is almost same way as creating a variable, but it is placed in the class block and begins with the “var” keyword, but the variable begins with a $ keyword

now we will try to insert the properties to the Mobil class that we just created above, open the Mobil.php file then edit the code as below

<?php
class Mobil
{
    var $nama; //the properties
    var $merek; //the properties
}

?>

After adding the properties/fields to the class as shown above, we can manipulate the properties/fields when creating the object.

To manipulate data properties/fields, it’s the same way as manipulate the  variables, while to access the properties/fields we need the keyword “->” after the name of the object and followed by the name of the fields/properties.

we will immediately manipulate the properties/fields that we created in the class above, open the object.php file and edit the code as below

<?php

require_once "data/Mobil.php"; //include file class Mobil.php 

$mobil  =   new mobil(); //create new object that name is mobil

$mobil->nama    = "Inova"; //we manipulate the properties/fields ($nama)
$mobil->merek   =   "Toyota"; //we manipulate the properties/fields ($merek)

var_dump($mobil);

?>

If we run the object.php in the VScode, it look like images below

insert the properties to the class and manipulate it – photo by webbaliseo

If you want to show the data of object with “echo” command you can edit the object.php like code below

<?php

require_once "data/Mobil.php"; //include file Mobil.php 

$mobil  =   new mobil(); //create new object that called mobil

$mobil->nama    = "Inova"; //we manipulate the properties/fields ($nama)
$mobil->merek   =   "Toyota"; //we manipulate the properties/fields ($merek)

var_dump($mobil);

echo "Data Object mobil" . PHP_EOL;
echo "==================" . PHP_EOL;
echo "Nama Mobil : $mobil->nama" . PHP_EOL; // show the properties nama 
echo "Nama Merek : $mobil->merek" . PHP_EOL; // show the properties merek 


?>

If we run the object.php in the VScode, it look like images below

use echo command to show the object data – photo by webbaliseo

That is the Object Oriented Programming PHP Tutorial part 1, I hope it will be useful, see you in the Object Oriented Programming PHP Learning Tutorial part 2 which will discuss type declaration properties, Default value properties, Nullable properties and functions in Object Oriented Programming.

Leave a Reply

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