Call to Private Method

Fatal error: Uncaught Error: Call to private method App\Entity\User::hashPassword() from scope App\Controller\UserController

Quick Answer

You are trying to call a private method from outside its class. Change the visibility to public or protected, or use a public method that internally calls the private one.

Why This Happens

PHP enforces visibility modifiers strictly. Private methods can only be called from within the same class. If another class or external code tries to call a private method, PHP throws this error. This encapsulation is intentional, so consider whether exposing the method is appropriate.

The Problem

class User {
    private function hashPassword(string $password): string {
        return password_hash($password, PASSWORD_DEFAULT);
    }
}
$user = new User();
$hash = $user->hashPassword('secret'); // Cannot access private method

The Fix

class User {
    public function setPassword(string $password): void {
        $this->passwordHash = $this->hashPassword($password);
    }

    private function hashPassword(string $password): string {
        return password_hash($password, PASSWORD_DEFAULT);
    }
}
$user = new User();
$user->setPassword('secret');

Step-by-Step Fix

  1. 1

    Understand the access restriction

    The method is private because the author intended it for internal use only. Determine if you should be calling it directly.

  2. 2

    Use a public interface

    Look for a public method that wraps the private logic, or create one if the functionality needs to be exposed.

  3. 3

    Change visibility if appropriate

    If the method genuinely needs external access, change private to public or protected, considering the impact on encapsulation.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free