Static methods can be accessed directly, without needing to instantiate the class.
They are defined using the static
keyword within the class.
<?php
class Greeting {
public static function sayHello() {
echo "Welcome to WebmasterMaze.";
}
}
?>
Example 1:
To invoke a static method, use the class name followed by the double colon (::) and then the method name.
<?php
Greeting::sayHello();
/*
Output:
Welcome to WebmasterMaze.
*/
?>
Example 2:
A class can contain both static and instance methods. A static method can also be called from another method within the same class using the self
keyword and the double colon (::).
<?php
class Message {
public static function greet() {
echo "Welcome to WebmasterMaze.";
}
public function __construct() {
self::greet();
}
}
new Message();
/*
Output:
Welcome to WebmasterMaze.
*/
?>
Example 3:
Static methods can also be accessed from methods in different classes. For this, the static method must be declared as public.
<?php
class Greeting {
public static function greet() {
echo "Welcome to WebmasterMaze.";
}
}
class Display {
public function showMessage() {
Greeting::greet();
}
}
$obj = new Display();
$obj->showMessage(); // Executes static method Greeting::greet(); defined in a different class
?>
Example 4:
To invoke a static method from a child class, use the parent
keyword within the child class. The static method can be either public or protected.
<?php
class Car {
protected static function getCarName() {
return "Mitsubishi Lancer";
}
}
class CarName extends Car {
public $name;
public function __construct() {
$this->name = parent::getCarName();
}
}
$car = new CarName();
echo $car->name;
/*
Output:
Mitsubishi Lancer
*/
?>
Static Properties
Static properties are declared using the static
keyword.
To access a static property, use the class name, followed by the double colon (::
), and then the property name.
Example 1:
<?php
class Website {
public static $siteName = "WebmasterMaze";
}
// Access static property (Classname::propertyname)
echo Website::$siteName;
?>
Example 2:
A class can contain both static and non-static properties. To access a static property from a method within the same class, use the self
keyword followed by the double colon (::
) and the property name.
<?php
class Website {
// Static property
public static $siteName = "WebmasterMaze";
// Non-static property
public $siteSlogan = "Welcome to WebmasterMaze";
// Method to access the static property
public function getSiteName() {
return self::$siteName;
}
}
// Accessing the static property directly using the class name
echo Website::$siteName; // Output: WebmasterMaze
// Creating an instance of the class
$website = new Website();
// Accessing the non-static property using the instance
echo $website->siteSlogan; // Output: Welcome to WebmasterMaze
// Accessing the static property using the instance method
echo $website->getSiteName(); // Output: WebmasterMaze
?>
Example 3:
To access a static property from a child class, use the parent
keyword within the child class.
<?php
class Website {
public static $siteName = "WebmasterMaze"; // Static property in the parent class
}
class Blog extends Website {
public function getSiteName() {
return parent::$siteName; // Use 'parent::' to access the parent's static property
}
}
// Access static property directly via the child class
echo Blog::$siteName; // Output: WebmasterMaze
// Access static property using the method in the child class
$blog = new Blog();
echo $blog->getSiteName(); // Output: WebmasterMaze
?>