Cara menggunakan php create static class

Pada kesempatan kali ini kami kursus web design jakarta akan menjelaskan cara menggunakan class static keyword di javascript. Perlu teman-teman ketahui sebelumnya, static adalah salah satu keyword atau kata kunci statis di javascript yang dapat kalian gunakan untuk membuat method statis dari suatu class di javascript. Method statis tersebut penggunaannya adalah dipanggil langsung di suatu class tanpa harus membuat instance atau object class terlebih dahulu.

Show

Seperti pada contoh studi kasus dari kode yang kami kursus web design jakarta berikan dibawah ini, dan kalian bisa salin kode dibawah ini :

index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Cara Menggunakan Class Static Keyword Di JavaScript</title>

</head>

<body>

<div class="output"></div>

</body>

<script>

class Mobil{

  constructor(merk) {

    this.namamobil = merk;

  }

  static hello() {  // static method

    return "Ini adalah mobil saya !!";

  }

}

mobilsaya = new Mobil("Ford");

document.getElementsByClassName("output")[0].innerHTML = Mobil.hello();

</script>

</html>

Dari output tersebut dimana kalian bisa lihat pada method static hello tidak membutuhkan instance atau object. Sampai disini penjelasan saya mengenai cara menggunakan class static keyword di javascript,

Semoga bermanfaat dan dapat langsung dipraktekkan yah, jangan lupa ikuti terus artikel dari kami kursus web design Dumet School Jakarta

Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).



PHP - More on Static Methods

A class can have both static and non-static methods. A static method can be accessed from a method in the same class using the self keyword and double colon (::):

Example

class greeting {
  public static function welcome() {
    echo "Hello World!";
  }

  public function __construct() {
    self::welcome();
  }
}

new greeting();
?>

Try it Yourself »

Static methods can also be called from methods in other classes. To do this, the static method should be public:

Example

class greeting {
  public static function welcome() {
    echo "Hello World!";
  }
}

class SomeOtherClass {
  public function message() {
    greeting::welcome();
  }
}
?>

Try it Yourself »

To call a static method from a child class, use the parent keyword inside the child class. Here, the static method can be public or protected.