PHP – Array, Object, Built in PHP Feature

PHP – Array, Object, Built in PHP Feature

Pada bahasa pemrograman PHP sendiri penggunaan/inisialisasi array tidak jauh berbeda dengan bahasa pemrograman lain, selain itu PHP sendiri sudah berorientasi Object dengan ciri sebuah object berupa syntax class dengan method berupa function didalamnya beserta hak aksesnya atau bisa disebut User-Defined Function(UDF). PHP sendiri memiliki beberapa fungsi bawaan yang disebut Built in Feature seperti sqrt() untuk perpangkatan atau mengganti text dengan str_replace(). Untuk array assosiatif pada PHP lebih mirip dengan HashMap pada java yang menyertakan key dan value dari array. Untuk modularisasi PHP menggunakan syntax include yang bisa dipakai berulang kali dan require yang hanya bisa dipakai sekali.


Untuk lebih lengkapnya berikut adalah Source code untuk implementasi inisialisasi array, pembuatan dan pemakaian Object, serta pemakai fitur bawaan PHP yang diselipkan pada fungsi Object. Untuk keterangannya bisa dilihat pada tag comment.

Source Code: ArrayModul.php

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
 class ArrayModul {
  public function ArrayModul() {}

  public function Space() {
   echo "<br />";
  }

  //Pembuatan function dengan format: HakAkses, function, nama function, (parameter)
  public function PrintArray($arr_data, $withSpace) {
   $this->Space();
   $count = 0;

   for($i = 0; $i < sizeof($arr_data); $i++) {
    if($withSpace) {
     echo "Data[$i]: $arr_data[$i]";
     $this->Space();
    } else {
     if($count == 0) {
      echo "ArrayData: ";
      $count++;
     }

     echo "$arr_data[$i] ";
    }
   }
  }

  public function PrintMultidimensionArray($arr_data) {
   for($i = 0; $i < sizeof($arr_data); $i++) {
    for($j = 0; $j < sizeof($arr_data[$i]); $j++) {
     echo $arr_data[$i][$j]." ";
    }

    $this->Space();
   }
  }

  public function BuiltInMath($value, $operator) {
   $result;

   switch($operator) {
    //Perpangkatan
    case 'pow':
     $result = pow($value, 2);
     break;

    //Pengakaran
    case 'sqrt':
     $result = sqrt($value);
     break;

    //Pembulatan kebawah
    case 'floor':
     $result = floor($value);
     break;

    //Pembulatan keatas
    case 'ceil':
     $result = ceil($value);
   }

   return $result;
  }

  public function BuiltInString($text, $sign, $replacedText) {
   if($replacedText == null && $sign == null) {
    echo "Upper Case: ".strtoupper($text)."<br />";
    echo "Lower Case: ".strtolower($text)."<br />";
   } else {
    echo "Replace Text: ".str_replace($sign, $replacedText, $text)."<br />";
   }
  }

  public function SearchMax($arr_data) {
   for($i = 0; $i < sizeof($arr_data); $i++) {
    for($j = $i+1; $j < sizeof($arr_data); $j++) {
     if($arr_data[$i] > $arr_data[$j]) {
      $temp = $arr_data[$i];
      $arr_data[$i] = $arr_data[$j];
      $arr_data[$j] = $temp;
     }     
    }
   }

   return $arr_data[sizeof($arr_data) - 1];
  }

  public function ArithmaticOperation($a, $b, $operator) {
   $result;

   //Penggunaan Switch Case
   switch ($operator) {
    case '+': $result = $a + $b; break;
    case '-': $result = $a - $b; break;
    case '/': $result = $a / $b; break;
    case '*': $result = $a * $b; break;
    default: break;
   }

   $result = $result == null ? $result = 'Error' : $result;
   echo "Value1  : $a <br />";
   echo "Value2  : $b <br />";
   echo "Operator: $operator <br />";
   echo "Result  : $a $operator $b = $result";
  }

  public function MatrixOperation($matrixA, $matrixB) {
   $result;

   echo "MatrixA: <br />";
   $this->PrintMultidimensionArray($matrixA);
   $this->Space();

   echo "MatrixB: <br />";
   $this->PrintMultidimensionArray($matrixB);
   $this->Space();

   if(sizeof($matrixA) == sizeof($matrixB)) {
    for($i = 0; $i < sizeof($matrixA); $i++) {
     for($j = 0; $j < sizeof($matrixA); $j++) {
      $result[$i][$j] = $matrixA[$i][$j] + $matrixB[$i][$j];
     }
    }
   } else {
    echo "Size not match <br />";
   }

   echo "Result: <br />";
   $this->PrintMultidimensionArray($result);
  }
 }
?>


Penggunaan Object dan function: modul2.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
 //Contoh modularisasi
 require("ArrayModul.php"); 

 //Assignment array
 $var_arr = array(
  'Kuda',
  'Aduk',
  'Duka'
 );

 $var_arrB[0] = 'Kartu';
 $var_arrB[1] = 'Tukar';
 $var_arrB[2] = 'Ratuk';

 //Assignment Array Assosiatif
 $arr_arrAsso['Key1'] = 'Value1';
 $var_arrAsso['Key2'] = 'Value2';
 $var_arrAsso['Key3'] = 'Value3';

 //Pembuatan Object
 $ArrModul = new ArrayModul();

 //Pemanggilan fungsi pada Object
 $ArrModul->PrintArray($var_arrB, true);

 //Fungsi BuiltInMath/fungsi math bawaan PHP
 $ArrModul->Space();
 echo "Pangkat: ".$ArrModul->BuiltInMath(3, 'pow')."<br />";
 echo "Akar   : ".$ArrModul->BuiltInMath(4, 'sqrt')."<br />";
 echo "Floor  : ".$ArrModul->BuiltInMath(3, 'floor')."<br />";
 echo "Ceil   : ".$ArrModul->BuiltInMath(4, 'ceil')."<br />";

 //Fungsi BuiltInString/operasi string bawaan PHP
 $ArrModul->Space();
 $ArrModul->BuiltInString("Welcome to the jungle!", null, null);
 $ArrModul->BuiltInString("Welcome to the jungle!", "jungle", "school");

 //Mencari nilai Maximum
 $arrValue = array(46, 23, 12, 76, 23, 14, 1, 0, 15);
 $ArrModul->PrintArray($arrValue, false);
 $ArrModul->Space();

 //Penggunaan Function dengan return value
 echo "Maximum Value: ".$ArrModul->SearchMax($arrValue)."";

 //Include Header
 include("header.php");
 
 //Simple Calc
 $ArrModul->ArithmaticOperation(5, 12, '+');
 
 //Include Footer
 include("footer.php");

 //ArrayMultidimension
 $MatrixA = array(
  array(3, 5),
  array(9, 2)
 );

 $MatrixB = array(
  array(9, 12),
  array(7, 18)
 );

 //Matrix Operation
 $ArrModul->MatrixOperation($MatrixA, $MatrixB);

?>



File include: header.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
 echo "
  <hr>
   <center>
    Simple Include Header
   </center>
  <hr>
 ";

?>



File include: footer.php

1
2
3
<?php
 echo "<hr>"
?>



Output:

Komentar