I've made a class that acts like an file wrapper. When user call delete method, i want to unset the object (actually $this). Is there a way (workaround) to do that? The manual said no, there is no way...
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
The manual is right. There is no way.
unset
is used essentially to say "I'm finished using a particular variable; it can be considered for garbage collection as far as I'm concerned.The trouble is that
$this
only makes sense in the context of a method call anyway. Once the method completes,$this
will fall out of scope (essentially) and will no longer count as a reference to the object. (For practical purposes.)So you don't need to worry about freeing up
$this
. PHP will take care of that.What you're probably trying to do is
unset
the other variables that might reference the same instance. And unless you have access to those from within the method, you can't touch them, much lessunset
them.I spent a lot of time finding a way of setting to NULL an object from within it's self.
Please try this code:
Trick is $this won't work, you need to pass it the actual reference by reference to the class.
Another way to access the actual reference without passing it is defining the object as a global:
Well, No, only the other way around, you can use the magic __destruct() function:
This will be triggered when someone unset() the Object AND when the script ends. But an Object can't destroy itself.
As far as I know, there is no way to destroy a class instance from within the class itself. You'll have to unset the instance within its scope.
Somewhat related: Doing the above will automatically call any existing
__destruct()
magic method of the class so you can do any additional cleanup. More infoI has the same problem as you, in some point I want to make the object remove itself but it isn't possible in php, so this is a workaround that may help you.
You can setup a private flag and check that flag in every method that require the object still exists, something like:
Other way could be overloading the _call method and review the flag before calling the method, this also may require renaming the methods with some prefix like "pub" to make the call inside the __call method. This is an example:
IMHO it's a real problem that an object cannot remove itself from memory, but it's part of the design of PHP, it's not C++. As a left thought, for this and other things, you could relay the logic of the application in other language/libraries developed in other languages like C++/C# and use PHP to consume that logic, something like "HTML/CSS/JS/PlainText <-> PHP <-> C# Cli <-> DB/Files/Storage"
Put your delete code in the destructor if you want to get rid of the object and the file it wraps at the same time. Then just
unset
the object instead of calling a delete method.