Skip to main content

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.

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).
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.
$this->standardMethod();
  1. External Object Context: Accessed using the object operator (->) on an instantiated object variable.
$instance = new AccessDemonstration();
$instance->standardMethod();
  1. Static Context: If the method is also declared static, it is accessed using the Scope Resolution Operator (::) directly on the class name.
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.
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.
Master PHP with Deep Grasping Methodology!Learn More