Skip to content

Attempting to initialize class with private constructor calls destructor #14456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Girgias opened this issue Jun 3, 2024 · 2 comments
Closed

Comments

@Girgias
Copy link
Member

Girgias commented Jun 3, 2024

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

@Girgias
Copy link
Member Author

Girgias commented Jun 3, 2024

The issue is that in zend_std_get_constructor we do not call zend_object_store_ctor_failed after raising an Error with zend_bad_constructor_call.

Girgias added a commit to Girgias/php-src that referenced this issue Jun 4, 2024
Girgias added a commit that referenced this issue Jun 6, 2024
Girgias added a commit that referenced this issue Jun 6, 2024
* PHP-8.2:
  Fix bug GH-14456: Attempting to initialize class with private constructor calls destructor
Girgias added a commit that referenced this issue Jun 6, 2024
* PHP-8.3:
  Fix bug GH-14456: Attempting to initialize class with private constructor calls destructor
@Girgias
Copy link
Member Author

Girgias commented Jun 6, 2024

Was fixed by cdb7677

@Girgias Girgias closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant