Static Member VariablesFor situations where you would like to associate some data with an entire class of objects, but the data is not fixed (in other words, constant), you can create member variables that are global to the entire class, not just specific instances. In PHP, these are called static member variables. They are declared by prefixing their declarations with the keyword Show <?php class User { public static $x; } You would declare static member variables just like any other; you can
specify a visibility modifier (public, private, or protected) with it and assign it an initial value. For the previous example, class <?php class User { public static $x = 10; public function reset() { self::$x = 0; } } User::$x++; echo User::$x . ' '; # Prints: 11 User::$x = 100; echo User::$x . ' '; # Prints: 100 $u = new User(); $u->reset(); Unlike class constants, static member variables are not read-only. Accessing the value of a static variable is similar to accessing a class constant: You either use the type name: In the previous example, to use the As a more interesting example, I can use a static member variable to count the number of instances of a class that have been created: <?php class User { private static $x = 0; protected $username; public function __construct ($u = 'Guest') { $this->username = $u; self::$x++; echo 'Instances created: '. self::$x .'<br>'; } public function getUsername() { return $this->username; } } $user1 = new User('Admin'); # Prints: Instances created: 1 $user2 = new User(); # Prints: Instances created: 2 $user3 = new User('BrainBell'); # Prints: Instances created: 3 There will also come a time when you want to associate methods with your type that do not necessarily operate on a specific instance but are broad operations related to that type. For example, I might want to define a method on the <?php class User { protected static $users = []; protected $username; public function __construct($u) { $this->username = $u; self::$users[$u] = $this; # Store each instance } public function getUsername() { return $this->username; } public static function getAllInstances() { return self::$users; } } # The new instances are not assigned to variables new User('Admin'); new User('BrainBell'); new User('Guest'); $users = User::getAllInstances(); foreach($users as $user) echo $user->getUsername() . ', '; # Prints: Admin, BrainBell, Guest, Static
methods are allowed to have a visibility modifier of :: vs. -> and self vs. $thisFor people confused about the difference between
PHP OOP Tutorials:
Apa itu static pada PHP?Static property dan static method adalah property dan method yang langsung dapat diakses dari class tanpa instansiasi class (pembuatan object) terlebih dahulu. Cara mendeklarasikannya yaitu property atau method diawali menggunakan keyword static .
Apa itu method static?Static method di dalam python adalah sebuah fungsi atau method pada sebuah kelas yang bersifat statis. Bersifat statis di sini berarti kita bisa memanggil fungsi tersebut secara langsung tanpa harus membuat instan dari kelas yang bersangkutan.
Apa itu static pada OOP?Pengertian Perintah Static
Dalam bahasa Java, static adalah perintah khusus yang memungkinkan sebuah property atau method diakses langsung tanpa melalui object, tapi cukup menulis nama class saja. Dalam kode program ini, class Laptop di definisikan pada baris 1-5.
Sebutkan Untuk apa kita menggunakan static keyword?Keyword Static di java digunakan untuk mengakses member baik variable ataupun method ( prosedur atau fungsi ) pada class tertentu tanpa harus membuat suatu objek dari class itu.
|