> ## 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 Public Method

A public method in PHP is a class-bound function declared with the `public` access modifier, granting it unrestricted visibility within the application's execution context. It can be invoked from within the defining class, from derived (child) classes, and from the global scope via an instantiated object reference or, if declared static, directly via the class name.

## Syntax and Declaration

In PHP, methods are declared public using the `public` keyword preceding the `function` keyword. If an access modifier is entirely omitted, PHP's engine defaults the method's visibility to `public`, though explicit declaration is required by modern coding standards (PSR-12).

```php theme={"dark"}
class AccessDemonstration {
    // Explicit public method declaration
    public function standardMethod(): string {
        return "Accessible globally";
    }

    // Implicit public method (modifier omitted - not recommended)
    function legacyMethod(): void {
        // Defaults to public visibility
    }

    // Public method combined with static modifier
    public static function staticMethod(): void {
        // Accessible without object instantiation
    }
}
```

## Access Mechanics

The `public` modifier imposes no restrictions on the call stack origin. The method can be accessed via three primary mechanisms depending on its context:

1. **Internal Object Context:** Accessed using the `$this` pseudo-variable from within the same class or inherited classes.

```php theme={"dark"}
$this->standardMethod();
```

2. **External Object Context:** Accessed using the object operator (`->`) on an instantiated object variable.

```php theme={"dark"}
$instance = new AccessDemonstration();
$instance->standardMethod();
```

3. **Static Context:** If the method is also declared `static`, it is accessed using the Scope Resolution Operator (`::`) directly on the class name.

```php theme={"dark"}
AccessDemonstration::staticMethod();
```

## Inheritance and Overriding Rules

Public methods are fully inherited by any subclass extending the parent class. When dealing with inheritance, PHP enforces strict visibility rules based on the Liskov Substitution Principle:

* **Visibility Restriction:** A child class overriding a `public` method cannot restrict its visibility. An overridden `public` method cannot be redefined as `protected` or `private` in the subclass.
* **Signature Compatibility:** The overriding method must maintain signature compatibility with the parent's public method. It must accept at least the same required parameters, but PHP explicitly allows the child class to add new, optional parameters. The signature must also adhere to covariance for return types and contravariance for parameter types.

```php theme={"dark"}
class ParentClass {
    public function executeTask(string $data): void {}
}

class ChildClass extends ParentClass {
    // Valid: Visibility remains public, and the new parameter is optional
    public function executeTask(string $data, bool $force = false): void {}
    
    // Fatal Error: Access level to ChildClass::executeTask() must be public (as in class ParentClass)
    // protected function executeTask(string $data): void {} 
}
```

## Modifier Combinations

The `public` keyword can be combined with other method modifiers to alter execution behavior without changing visibility:

* `public final function`: The method is globally accessible but cannot be overridden by any child class.
* `abstract public function`: Declared within an `abstract` class. It enforces that any concrete child class must implement the method with `public` visibility.
* `public static function`: Binds the method to the class itself rather than an instance of the class, while maintaining global accessibility.

<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>
