Object Oriented Programming

Object Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.” For example, consider a remote-controlled toy car: the car itself is the object, and the remote control is the tool used to operate it. The RC car can be controlled to move forward, backward, left, and right through the remote, which illustrates the basic concept of object-oriented programming. Programming languages like PHP support OOP through features like functions.

Function

Before understanding functions, it’s helpful to revisit the concept of variables. Functions are similar to variables in that while a variable stores a value, a function stores a block of code. In PHP, a function is a section of code designed to perform a specific task, separate from the program that calls it. In complex programs with multiple features, functions are essential for easier code readability and maintenance. With functions, there’s no need to write lengthy code in the main program; you can break it down into smaller parts and simply call the functions within the main program, thereby simplifying the main program’s structure.

Functions can have parameters to accept variables. When declaring parameters in a function, you can also include default arguments.

Functions can return values using the return keyword placed within the function’s code block to send a value back to the main program.

The main purpose of a function is to avoid code duplication, allowing code to be reused simply by calling the function’s name.

In PHP, there are several important aspects to consider when declaring a function:

  1. Using the return keyword (for functions that return a value).
  2. Having function parameters/arguments.
  3. Supporting parameters with initial values/default parameters.

Generally, functions are categorized into two types:

  1. Functions that do not return a value.
  2. Functions that return a value.

Functions That Do Not Return a Value (Void Functions)

These functions are designed to contain a block of code for the purpose of reducing the number of lines in the program. They are typically called independently within a program block, as shown in the example below:

1
2
3
4
5
6
7
8
9
10
<?php
// Example of a Void Function
// The purpose is to execute a block of code
function f_iniFungsi() { // Function declaration
echo("This is a void function in PHP"); // Inside the function's code block
}


f_iniFungsi(); // Calling the function independently | Main Program Block
?>

Functions That Return a Value

These functions return a specific value and are usually called and stored in a variable, as demonstrated below:

1
2
3
4
5
6
7
8
9
<?php
function fg_str_fungsiKembalian() { // Function declaration
return "This is a return function in PHP"; // Inside the function's code block
}

$g_str_text = fg_str_fungsiKembalian(); // Calling the function and storing its return value in variable

echo($g_str_text); // Printing the result
?>

Program Blocks Inside Functions

After understanding functions, their categories, and their usage, the next step is to explore the content within the function’s code block. Before diving into the details of a function’s code block, it’s helpful to grasp a basic concept related to this. For example, if you have a debit/credit card with a VISA/MASTERCARD logo, you can use that card for transactions both nationally and internationally. However, if your debit/credit card does not have a VISA/MASTERCARD logo, you can only use it for national transactions. This concept of access and feature restrictions applies similarly to function code blocks, both inside and outside of functions. Once you understand this concept, we will introduce global and local variables.

Global Variables

Global variables can be used in any code block (inside function code blocks or outside function declarations). Because they are global, global variables can be accessed and used anywhere, whether in the same file or in different files.

Local Variables

Local variables can only be used within a specific function/class code block. Because of their limited scope, they can only be utilized within the block where they are declared and cannot be used outside of that function block.

Examples of Functions:

Function with parameters that returns a value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$g_int_result = 0; // Declaring a Global Variable

function fg_int_addFunctionWithParameters($l_int_value1, $l_int_value2) { // Function Declaration with Parameters

global $g_int_result;

$l_int_result = $l_int_value1 + $l_int_value2; // Declaring a local variable to hold the result

// Outputting the value of the local variable in a function

// 1. First Method: Move it to a global variable (For C/C++ & PHP)
// $g_int_result = $l_int_result;
// echo(g_int_result);

// 2. Second Method: Return the local variable value (Works in Python, PHP and C/C++)
return $l_int_result;
}

//fg_int_addFunctionWithParameters(4, 5); // Method 1 (Store the sum in the global variable)
$g_int_result = fg_int_addFunctionWithParameters(5, 6); // Method 2 (Call the function and store the returned value in g_int_result)

echo($g_int_result);
?>

Function with default parameters/initial values that does not return a value:

1
2
3
4
5
6
7
8
9
<?php
function fg_functionSubtractWithParameters($l_int_value1 = 1, $l_int_value2 = 1) { // Function Declaration with Default Parameters

$l_int_result = $l_int_value1 + $l_int_value2;
echo($l_int_result);
}

fg_functionSubtractWithParameters(); // When not provided with values, the function will automatically use the default parameters: value1 as 1 and value2 as 1
?>

Recursive Function

A recursive function is a function that calls itself when declared. It is usually used as an alternative to loops, but it is rarely used because it consumes a significant amount of computer resources, causing the computer to work harder compared to using regular loop constructs.

Here is an example of a recursive function used to calculate factorials:

1
2
3
4
5
6
factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1
Thus, factorial(5) = 5 * 4 * 3 * 2 * 1, which results in 120.
1
2
3
4
5
6
7
8
9
10
11
12
<?php
function fg_int_factorial($l_int_value) {
if ($l_int_value <= 1) {
return $l_int_value;
} else {
return $l_int_value * fg_int_factorial($l_int_value - 1); // Calls itself
}
}

$l_int_factorial = fg_int_factorial(5);
echo($l_int_factorial);
?>

Function Exercise

Create a function that returns the results of addition, subtraction, division, multiplication, and modulus, with 2 parameters that have initial values (values can be arbitrary). Then, create a void function to print your name and where you are from at the top of the program. The program should run in a loop until the user decides to stop it. Here’s an example output:

OUTPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---------------------
| Azhar Rizki Zulma |
| Bintaro |
---------------------
Enter Menu (+|-|/|*|%|stop): + (Input by user)
Enter Value 1: 5 (Input by User)
Enter Value 2: 5 (Input by User)
The result of addition 5 + 5 is 10

Enter Menu (+|-|/|*|%|stop): - (Input by user)
Enter Value 1: 5 (Input by User)
Enter Value 2: 5 (Input by User)
The result of subtraction 5 - 5 is 0

Enter Menu (+|-|/|*|%|stop): stop (Input by user)
Program stopped. Thank you for using my program.