Would calling this function from a class. call the objects destructor?
public Dispose() {
unset($this);
}
or can you call the __destruct method in PHP?
or can you only unset the object from outside of the class in PHP?
Would calling this function from a class. call the objects destructor?
public Dispose() {
unset($this);
}
or can you call the __destruct method in PHP?
or can you only unset the object from outside of the class in PHP?
I did a little testing and it appears you cannot destroy an object that way. The unset($this) does not destroy it (I suspect it's a copy-by-reference of the object local in scope to the method), and if you try to overwrite it (e.g. "$this = null") you get a parse error that says you can't do that.
But yest, you can call the __destruct() method from the client code if desired.
NogDog is right.
Last year I tried destroyinh object from within class.
No matter what clever tricks I tried .. it was really hopeless.
I think we can compare this with a function trying to alter or delete itself.
It wont happen.
A class and methods and $this and $var declarations are not active/dynamic elements.
The object is created OUTSIDE. This is the active element.
$db = new Database();
The only way to remove $db object is at the OUTSIDE.
$this is no object. $this only an inside class handle, like a statement.
So, if you do not want to wait for the object to self-destroy at script end
or at exit()
$db = new Database();
unset($db);
Which was the solution I had to finally settle for, the result of my investigation last year.
Thanks for the info...
I am used to C++ and C# classes. PHP classes are still evolving, or taking a different route, not sure which. And just for completeness only, I just wanted to add a dispose or delete method similar to C#, C++.
i.e Email->Dispose(); rather than unset(Email);
I also think the magic methods should be re-thought, a get/set method more like C# would be more appropriate. (I can see a potential problem with the magic versions). Also some proper overloading would be nice! :rolleyes:
It does sound like you're after __destruct(). That is called whenever an object is no longer referenced anywhere. (Since reclamation happens immediately, the distinction between disposal and destruction that you get in C# doesn't arise. There is a garbage collector in PHP5.3, but that's intended to deal with circular references that aren't dealt with by conventional reference counting.)
I'm inclined to agree wrt get/set; the main problems I see are with controlling visibility and static vs. instance properties.
Just one last quirk:
private $headers = "hello world"; // Class Properties
unset($this->headers); // $headers is now no longer a class property (without using magic methods)
Q: Will the $headers class property still be avaiable if I use $this->headers = null; instead ? (the property seems avaiable and doesn't throw an error, There isn't any PHP Garbage Collection thats going to bite me in the ass using null instead)?
// Class Properties
When I see "class properties" I think of static ones (i.e., those that belong to the class, rather than to particular object instances).
No; no ass-biting. The gc only deals to values that can't be accessed anyway. Thing to keep in mind is that PHP is still loosely typed; if you suddenly say "$foo->newthing = 42;" then the object in $foo will just as suddenly have a new field called "newthing" (unless you have __set magic).
(Incidentally, I prefer using =null anyway; using unset() makes it look like I'm trying to wipe out the whole variable, not just empty out its value.)
Weedpacket;10947489 wrote:
// Class Properties
When I see "class properties" I think of static ones (i.e., those that belong to the class, rather than to particular object instances).
I would normally refer to them as Members and Static Members respectfully. The PHP manual refers to them as properties, but I concure, Object Properties would be more suited and Class Properties, again, respectfully.
Too many languages... Just give me an assembler, please.