Skip to content
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

__destruct magic method being called before lazy Proxy creates value #669

Open
odahcam opened this issue Jun 13, 2019 · 4 comments
Open

Comments

@odahcam
Copy link
Contributor

odahcam commented Jun 13, 2019

When the class gets destroyed in its life cycle, the __destruct method gets called and, if there's one implementation in the original class, the proxy class tries to execute the base class implementation. But looks like the proxy class is getting destroyed before creating a instance for the base class and, as there's no check for null values, we get the following error:

[Wed Jun 12 00:52:18 2019] ::1:59011 [500]: / - Uncaught Error: Call to a member function __destruct() on null in <Project-path>\var\proxies\ProxyManagerGeneratedProxy__PM__<CLASS_NAME>Generated8d9f8c89dc680a18cbf6f65f21952d52.php:55
Stack trace:
#0 <Project-path>\vendor\php-di\php-di\src\Proxy\ProxyFactory.php(60): ProxyManagerGeneratedProxy\__PM__\<CLASS_NAME>\Generated8d9f8c89dc680a18cbf6f65f21952d52->__destruct()
#1 <Project-path>\vendor\php-di\php-di\src\Proxy\ProxyFactory.php(74): DI\Proxy\ProxyFactory->createProxy('<CLASS_NAME>', Object(Closure))
#2 <Project-path>\vendor\php-di\php-di\src\Compiler\ObjectCreationCompiler.php(140): DI\Proxy\ProxyFactory->generateProxyClass('<CLASS_NAME>')
#3 <Project-path>\vendor\php-di\php-di\src\Compiler\ObjectCreationCompiler.php(39): DI\Compiler\Obje in <Project-path>\var\proxies\ProxyManagerGeneratedProxy__PM_<CLASS_NAME>Generated8d9f8c89dc680a18cbf6f65f21952d52.php on line 55

The project path and class full qualified names were hidden because it's a private project, where the class is related to database connectivity.

That class just access authorization data and creates a new connection instance. The __destruct method implementation exists to ensure the connection will be closed when the class gets destroyed.

In this scenario the class was never used and that's why I believe it's getting destructed before creating an instance for the original class.

@mnapoli
Copy link
Member

mnapoli commented Jun 14, 2019

That's interesting, thank you for reporting. It might be worth asking at https://github.com/Ocramius/ProxyManager which is the library that handles that.

@odahcam
Copy link
Contributor Author

odahcam commented Jun 14, 2019

Well, we have some issues there that will need me to take some time to read, I'm linking'em here:

Also, I registered the issue there, because I can't investigate right now: Ocramius/ProxyManager#468

@odahcam
Copy link
Contributor Author

odahcam commented Jun 14, 2019

I'm afraid we gonna have to handle that case by our own by using the same solution: Ocramius/ProxyManager#373 (comment)

@odahcam
Copy link
Contributor Author

odahcam commented Jun 17, 2019

I'm doing some debugging, the error occurs right after this method...

public function createProxy(string $className, \Closure $initializer) : LazyLoadingInterface
{
$this->createProxyManager();
return $this->proxyManager->createProxy($className, $initializer);
}

The class reported in the issue is in a __construct parameter of another class which is explicitly defined in the container definitions, like:

<?php

return [
    OTHER_CLASS_Interface::class => DI\autowire(OTHER_CLASS::class),
];

This is the call stack:

ProxyManagerGeneratedProxy\__PM__\NAMESPACE\CLASSNAME\Generated8d9f8c89dc680a18cbf6f65f21952d52->__destruct (/Project/path/var/proxies/ProxyManagerGeneratedProxy__PM__NAMESPACECLASSNAMEGenerated8d9f8c89dc680a18cbf6f65f21952d52.php:55)
DI\Proxy\ProxyFactory->createProxy (/Project/path/vendor/php-di/php-di/src/Proxy/ProxyFactory.php:60)
DI\Proxy\ProxyFactory->generateProxyClass (/Project/path/vendor/php-di/php-di/src/Proxy/ProxyFactory.php:74)
DI\Compiler\ObjectCreationCompiler->compileLazyDefinition (/Project/path/vendor/php-di/php-di/src/Compiler/ObjectCreationCompiler.php:140)
DI\Compiler\ObjectCreationCompiler->compile (/Project/path/vendor/php-di/php-di/src/Compiler/ObjectCreationCompiler.php:39)
DI\Compiler\Compiler->compileDefinition (/Project/path/vendor/php-di/php-di/src/Compiler/Compiler.php:233)
DI\Compiler\Compiler->compile (/Project/path/vendor/php-di/php-di/src/Compiler/Compiler.php:133)
DI\ContainerBuilder->build (/Project/path/vendor/php-di/php-di/src/ContainerBuilder.php:177)
NAMESPACE\MyApp->__construct (/Project/path/vendor/php-di/slim-bridge/src/App.php:20)
NAMESPACE\MyApp->__construct (/Project/path/src/Core/MyApp.php:46)

The error message:

Exception has occurred.
Error: Call to a member function __destruct() on null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants