Static Properties and Methods in Object-Oriented Programming (OOP)
In some cases, if you need to directly access properties or methods without wanting to create an object of the class, then the static method will help you. Let's learn about it.
I. Understanding Static in Object-Oriented Programming (OOP)
1. What is Static?
Static in object-oriented programming (OOP) is a static component that can be either a property or a method.
2. How to declare static
To declare a static method or property, we use the static keyword.
class ClassName {
// Declare static properties
public static $staticProp = "kiemtientuweb.com";
// Declare static method
public static function staticMethod() {
echo "Hello World!";
}
}
II. How to use static methods
A class can have both static methods and regular methods.
A static method can be accessed from another method within the same class using the self keyword and the scope resolution operator ::
class Hello {
public static function welcome() {
echo "Hello World!";
}
public function __construct() {
self::welcome(); //Way 1
Hello::welcome(); //Way 2
}
}
new Hello();
Static methods can also be called from other methods in different classes. However, the static method must have a public access scope.
class Hello {
public static function welcome() {
echo "Hello World!";
}
}
class Hi {
public function message() {
Hello::welcome();
}
}
To call a static method from a child class, we use the parent keyword inside the child class. And of course, the static method must have a public or protected access scope.
class Domain {
protected static function getWebsiteName() {
return "kiemtientuweb.com";
}
}
class MyDomain extends Domain {
public $websiteName;
public function __construct() {
$this->websiteName = parent::getWebsiteName();
}
}
$my_domain = new MyDomain();
echo $my_domain ->websiteName;
III. How to use static properties
Similar to static methods, we can have a class with both static and regular properties.
A static property can be accessed from another method within the same class using the self keyword and the scope resolution operator ::
class pi {
public static $value=3.14159;
public function staticValue() {
return self::$value;
}
}
$pi = new pi();
echo $pi->staticValue();
To call a static property from an inherited child class, use the parent keyword inside the child class.
class pi {
public static $value=3.14159;
}
class x extends pi {
public function xStatic() {
return parent::$value;
}
}
// Get value of static property directly via child class
echo x::$value;
// or get value of static property via xStatic() method
$x = new x();
echo $x->xStatic();
To summarize, there are three ways to call a static property or method:
- To call a static property or static method within a regular method of the same class, use the self keyword and :: or the class name and ::
- To call a static property or static method outside unrelated classes, use the class name and ::
- To call a static property or static method inside an inherited class, use the parent keyword and ::
Note: A static property functions like a global variable; wherever it is processed in any file (within the same program), it retains the last value that was assigned to it in the class. Look at the two examples below for better understanding.
Here is an example code snippet without using static:
class Person
{
private $name = 'Ha Anh Tuan';
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
//Initialize person object
$person = new Person();
$person->setName('Hoang Thuy Linh');
echo $person->getName(); //Result: Hoang Thuy Linh
//create another object
$student = new Person();
echo $student->getName(); // Result: Ha Anh Tuan
And a code snippet using static:
class Person
{
private static $name = 'Ha Anh Tuan';
public function setName($name)
{
self::$name = $name;
}
public function getName()
{
return self::$name;
}
}
//Initialize person object
$person = new Person();
$person->setName('Hoang Thuy Linh');
echo $person->getName(); //Result: Hoang Thuy Linh
//create another object
$student = new Person();
echo $student->getName(); // Result: Hoang Thuy Linh
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 726 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 656 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 634 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 560 view
-
Call of Duty: Black Ops 6 Consumes 300GB SSD: Xbox Series X/S Gamers Take Note
09-02-2024 . 521 view
0 feedback