![]() |
So whats so good about __construct()?
PHP Code:
|
__construct() is the PHP5 equivalent to what the function 'classname' did in PHP4. So just think of it as an updated naming scheme for it, to describe what it does.
|
possibly, yet the __destruct() is nice, isn't there a way to do that in php 4? I forgot. don't you use the ~classname(){}
|
As far as I know, there is no equivalent to __destruct() in PHP4. If you need to use it, I'd recommend asking your host to upgrade to PHP5 or see if you can add a line in your .htaccess file to parse .php files as PHP5.
|
Here is a way to apparently mimic the destructor in PHP4. Albeit not the most attractive solution. PHP5 is certainly the way to go!
PHP Code:
|
Quote:
|
Quote:
|
Quote:
|
Destruct is called anyway when a script ends and so unsetting variables is quite unnecessary. Though many coders do like:
php Code:
It's not required at all. I've used it a few times to perform further actions when objects are destroyed, but apart from that, I stay clear. |
The reason the class-name-function was replaced by __construct() is to be more a genuine OOP language. For backwards compatibility they left the old-style in PHP 5.
I believe the class-name-function is removed from PHP 6. More info about it on http://www.php.net/manual/en/language.oop5.decon.php |
Yes it does the same thing but OOP is about maintaining code easily, so when you change the classname you don't need to change the name of the function that constructs the class.
|
Quote:
Code:
classname() {}Personally i think they did it just to confuse people :), or as you say the class name is hideously long. Just as a note a destructor in C++ is Code:
~classname() {}PHP4 didn't have a native destructor method, unless you bodge something together with register_shutdown_function as wildhoney said. |
| All times are GMT. The time now is 10:06 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0