> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Static Method

A static method in PHP is a class method bound to the class itself rather than to a specific instantiated object of that class. Because it operates within the class scope rather than the object scope, it can be invoked directly without requiring object instantiation.

## Declaration Syntax

Static methods are defined using the `static` keyword. By convention and strict standard, the `static` declaration follows the visibility modifier (`public`, `protected`, or `private`).

```php theme={"dark"}
class Calculator {
    public static function add(int $a, int $b): int {
        return $a + $b;
    }
}
```

## Invocation Mechanisms

Static methods are accessed using the Scope Resolution Operator (`::`), formally known as the Paamayim Nekudotayim.

**Standard External Invocation:**
When calling the method from outside the class, use the literal class name followed by the scope resolution operator.

```php theme={"dark"}
$result = Calculator::add(5, 10);
```

**Dynamic Invocation:**
PHP supports dynamic static method calls where the class name is stored in a variable.

```php theme={"dark"}
$className = 'Calculator';
$result = $className::add(5, 10);
```

**Internal Invocation (`self::`):**
When calling a static method from within another method of the same class, use the `self` keyword to reference the current class scope.

```php theme={"dark"}
class Calculator {
    public static function add(int $a, int $b): int {
        return $a + $b;
    }

    public static function addThree(int $a, int $b, int $c): int {
        return self::add($a, $b) + $c;
    }
}
```

**Parent Invocation (`parent::`):**
When a child class overrides a static method, the overridden method in the parent class can still be invoked using the `parent` keyword.

```php theme={"dark"}
class AdvancedCalculator extends Calculator {
    public static function add(int $a, int $b): int {
        // Call the parent's static method
        $baseSum = parent::add($a, $b);
        return $baseSum;
    }
}
```

## Technical Constraints and Behavior

**1. Absence of Object Context (`$this`)**
Because static methods are not bound to an instance, the pseudo-variable `$this` is not available inside a static method. Attempting to use `$this` within a static method will throw a fatal error.

**2. Property Access**
Static methods cannot access non-static (instance) properties *without an explicit object context*. However, they can access and modify instance properties if they instantiate an object internally or have an object instance passed to them as an argument.

```php theme={"dark"}
class Configuration {
    private static string $environment = 'production';
    private string $version = '1.0';

    public static function getEnvironment(): string {
        // Valid: Accessing a static property
        return self::$environment; 
    }

    public static function getVersion(Configuration $config): string {
        // Valid: Accessing an instance property via an explicit object context
        return $config->version; 
        
        // Fatal Error: Cannot use $this->version
    }
}
```

**3. Late Static Binding and the `static` Return Type**
PHP resolves the `self::` keyword at compile-time, meaning it always references the class where the method was explicitly defined. To allow static methods to reference the class that was called at runtime (which is critical in inheritance hierarchies), PHP provides Late Static Binding via the `static::` keyword.

Additionally, PHP 8.0 introduced the `static` return type. This is used to indicate that a static method returns an instance of the late-bound called class, rather than the defining class.

```php theme={"dark"}
class ParentClass {
    public static function getName(): string {
        return "Parent";
    }

    public static function printName(): void {
        echo self::getName();   // Early binding (resolves to ParentClass)
        echo static::getName(); // Late static binding (resolves to called class)
    }

    // PHP 8.0+ static return type
    public static function create(): static {
        return new static(); 
    }
}

class ChildClass extends ParentClass {
    public static function getName(): string {
        return "Child";
    }
}

ChildClass::printName(); 
// Output: ParentChild

$instance = ChildClass::create(); 
// $instance is of type ChildClass, not ParentClass
```

**4. Instance Invocation**
While PHP allows a static method to be called from an instantiated object using the object operator (`$object->staticMethod()`), this is considered an anti-pattern. Strict standards dictate that static methods should exclusively be called using the scope resolution operator (`ClassName::staticMethod()`) to maintain clear architectural boundaries between class scope and object scope.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
