In addition to the usual methods and properties, classes can also contain static methods and properties that are denoted keyword – static. Static methods and properties are created once for the entire class and apply to the entire class. For the usual properties and methods, a separate copy is created for each object.
Let’s create test static properties:
class Person//create a new class { public $name, $age;//create regular properties with a public access modifier // create a static property, this is the retirement age, this property will not change and will be available to the whole class regardless of its object static $retirenmentAge = 70; function __construct($name, $age) { $this->name = $name; $this->age = $age; } function sayHello() { echo self::$retirenmentAge; } // create a static object method that is also available to the whole class and not to a specific object of the Person class static function printPerson($person) { echo "Name: $person->name age: $person->age<br>"; } }
When accessing static methods and properties, the class name and the :: operator are used instead of the -> access operator, so how a static method applies to the entire class, not to a specific object of that class.
Examples of calling static property and static method:
<?php //to begin with we create object of a class $tom=new Person("Billy", 33); //now turn to the class method $tom->sayHello(); //call the static method, it does not require a new class object to create it Person::printPerson($tom); ?>
To refer to static properties and methods within a class, can be used the self keyword:
Examples of using
Static methods and properties in PHP have several uses:
- General data
- Utilitarian functions
- Static methods can be used to create utility functions that do not require instantiation of the class. This can be useful for grouping related functions within a class, or for providing convenient access to class functionality without having to create an object.
class MathUtils { public static function double($number) { return $number * 2; } public static function square($number) { return $number * $number; } } echo MathUtils::double(5); // Return: 10 echo MathUtils::square(3); // Return: 9
- Factory Methods
- Static methods can be used to create objects of a class or its subclasses. Factory methods provide a convenient way to create objects using specific logic or settings.
class Car { public $brand; private function __construct($brand) { $this->brand = $brand; } public static function create($brand) { return new self($brand); } } $car = Car::create("Toyota"); echo $car->brand; //Return: Toyota
- Static properties can be used to store general data that should be available to all instances of the class. For example, you can use a static property to count the number of objects of a class created, or to store information that should be common to all instances.
class Counter { public static $count = 0; public function __construct() { self::$count++; } } $obj1 = new Counter(); $obj2 = new Counter(); echo Counter::$count; //Returns: 2