Skip to content

Attempting to initialize class with private constructor calls destructor #14456

Closed
@Girgias

Description

@Girgias

Description

Found while working on #14440

The following code:

<?php
class PrivateUser {
    private function __construct() {
        return new stdClass();
    }
    public function __destruct() {
        echo 'Destructor for ', __CLASS__, PHP_EOL;
    }
}

new PrivateUser();

Resulted in this output:

Destructor for PrivateUser

Fatal error: Uncaught Error: Call to private PrivateUser::__construct() from global scope in /in/EF0hE:12
Stack trace:
#0 {main}
  thrown in /in/EF0hE on line 12

But I expected this output instead:

Fatal error: Uncaught Error: Call to private PrivateUser::__construct() from global scope in /in/EF0hE:12
Stack trace:
#0 {main}
  thrown in /in/EF0hE on line 12

PHP Version

master

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions