Skip to content

Defining and Accessing Static Class Members in PHP

To define a static function, use the static keyword in the method declaration within a class:

class MyClass {
public static function myStaticMethod() {
return "Hello, I'm a static method!";
}
}
php

Static methods can be accessed directly using the class name followed by the scope resolution operator ::

echo MyClass::myStaticMethod(); // Outputs: Hello, I'm a static method!
php

Static methods can also be called from within other static methods of the same class:

class MyClass {
public static function myStaticMethod() {
return "Hello, I'm a static method!";
}
public static function anotherStaticMethod() {
// Calling the static method from another static method
return self::myStaticMethod();
}
}
echo MyClass::anotherStaticMethod(); // Outputs: Hello, I'm a static method!
php

Static methods can accept parameters just like regular methods:

class MathOperations {
public static function add($a, $b) {
return $a + $b;
}
}
echo MathOperations::add(5, 3); // Outputs: 8
php

Accessing Static Functions from Other Classes

Section titled “Accessing Static Functions from Other Classes”

You can also call static methods from other classes:

class Utility {
public static function getGreeting() {
return "Hello from Utility!";
}
}
class Client {
public function showGreeting() {
// Calling static method from another class
return Utility::getGreeting();
}
}
$client = new Client();
echo $client->showGreeting(); // Outputs: Hello from Utility!
php
class BaseClass {
public static function staticMethod() {
return "BaseClass static method";
}
}
class DerivedClass extends BaseClass {
public static function staticMethod() {
return "DerivedClass static method";
}
}
echo BaseClass::staticMethod(); // Outputs: BaseClass static method
echo DerivedClass::staticMethod(); // Outputs: DerivedClass static method
php

Here’s a simple class with a static method:

class MathUtils {
public static function add($a, $b) {
return $a + $b;
}
}
// Calling the static method without creating an instance
$result = MathUtils::add(5, 10);
echo $result; // Outputs: 15
php

2. Static Method with Visibility Modifiers

Section titled “2. Static Method with Visibility Modifiers”

Static methods can have visibility modifiers like public, protected, and private.

class User {
private static $count = 0;
public static function incrementCount() {
self::$count++;
}
public static function getCount() {
return self::$count;
}
}
// Using the static methods
User::incrementCount();
User::incrementCount();
echo User::getCount(); // Outputs: 2
php

Static methods can accept parameters just like regular methods.

class Converter {
public static function toUpperCase($string) {
return strtoupper($string);
}
}
// Calling the static method with a parameter
echo Converter::toUpperCase("hello world"); // Outputs: HELLO WORLD
php

Static methods are inherited but are not polymorphic. This means that calling a static method from a subclass does not override the method in the parent class.

class Animal {
public static function sound() {
return "Some generic sound.";
}
}
class Dog extends Animal {
public static function sound() {
return "Bark!";
}
}
// Calling static methods
echo Animal::sound(); // Outputs: Some generic sound.
echo Dog::sound(); // Outputs: Bark!
php

5. Static Methods with self and static Keywords

Section titled “5. Static Methods with self and static Keywords”
  • self refers to the class in which the method is defined.
  • static refers to the class that was used to call the method.
class Base {
public static function identify() {
return "Base";
}
public static function callIdentify() {
return self::identify();
}
}
class Derived extends Base {
public static function identify() {
return "Derived";
}
}
echo Base::callIdentify(); // Outputs: Base
echo Derived::callIdentify(); // Outputs: Derived
php

In this example, self::identify() in Base::callIdentify() calls the identify() method of the Base class, even if called from a subclass like Derived.

Static methods can be used to return constant values.

class Config {
const VERSION = '1.0';
public static function getVersion() {
return self::VERSION;
}
}
// Accessing the constant and static method
echo Config::getVersion(); // Outputs: 1.0
php

Static methods can interact with static properties.

class Counter {
private static $count = 0;
public static function increment() {
self::$count++;
}
public static function getCount() {
return self::$count;
}
}
// Incrementing and accessing the count
Counter::increment();
Counter::increment();
echo Counter::getCount(); // Outputs: 2
php

Static methods can also be used within namespaces, just like in the global namespace.

namespace Utilities;
class Helper {
public static function sayHello() {
return "Hello from Utilities namespace!";
}
}
// Using the static method
echo \Utilities\Helper::sayHello(); // Outputs: Hello from Utilities namespace!
php