-
-
Notifications
You must be signed in to change notification settings - Fork 189
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
[From PHP-DI] __destruct
magic method being called before lazy Proxy creates value #669
#468
Comments
This is very likely a duplicate of #373 |
Yes, looks like it is, but I would like to discuss it again if possible, starting with: why the instance inside the proxy-generated class is never checked for null values before being called? I think this is a serious problem, the proxy class should check if the instance value exists before calling it. The proxy should do that at least in the magic methods, cause there's no way to guarantee the magic method is being called by the user code or by some PHP magic. Just my thoughts. I'm confident it matters for @mnapoli too, as I'm experiencing this trough his lib. |
In theory, the initializer should have been called. The only reason why this could fail is a failure to initialise the property: that should lead to an exception to be thrown, but due to the weird semantics of Do you maybe have an example of the class that was proxied, and its initializer? |
This is the proxied class, attention in <?php
declare(strict_types=1);
namespace SOME_NAMESPACE;
/**
* @Injectable(lazy=true)
*/
class CLASS_NAME implements INTERFACE_IMPLEMENTATION
{
/**
* {@inheritdoc}
*/
public function __construct()
{
// code omitted
}
/**
* {@inheritdoc}
*/
public function closeConnection(): void
{
// code omitted
}
/**
* {@inheritdoc}
*/
public function __destruct()
{
$this->closeConnection();
}
}
|
Can I don't know which kind of proxy is being used in the upstream library though. |
Well, construct is just setting a property, like: <?php
class {
/**
* {@inheritdoc}
*/
public function __construct(
ContainerInterface $container
) {
$this->container = $container;
// The connection configuration.
$this->connectionParams = [/* data */] ?? [/* other data */ ];
}
} I would say it doesn't throws, because no integrations tests failed while in development, but when in production, where the PHP-DI container gets compiled, the error shows up. |
Do we have any news about this issue ? |
@anologicon I'd need an example where this fails in an integration test, as noted in #468 (comment) |
Also needed: proxy type in use. |
I don't know if I've implemented it right, but there it is: #471 |
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: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.
The text was updated successfully, but these errors were encountered: