TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   So whats so good about __construct()? (http://www.talkphp.com/general/1378-so-whats-so-good-about-__construct.html)

Nor 11-04-2007 10:37 PM

So whats so good about __construct()?
 
PHP Code:

<?php
class construction
{
    function 
__construct()
    {
        echo 
"Sup";
    }
}
class 
construction_2
{
    function 
construction_2()
    {
        echo 
"<br />Sup 2";
    }
}
$c1 = new construction;
$c2 = new construction_2;
?>

Does the same thing, I don't see any advantages in it. Unless the class name is extremely long.

Andrew 11-05-2007 04:08 AM

__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.

Nor 11-05-2007 05:13 AM

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(){}

Andrew 11-05-2007 10:19 PM

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.

Wildhoney 11-05-2007 10:44 PM

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:

register_shutdown_function(array(&$this'__destruct')); 


Nor 11-05-2007 11:12 PM

Quote:

Originally Posted by Andrew (Post 3651)
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.

lmao, we got php 5.2.3, I just asked a basic question.

Andrew 11-05-2007 11:31 PM

Quote:

Originally Posted by Nor (Post 3654)
lmao, we got php 5.2.3, I just asked a basic question.

Well, how was I supposed to know that? You just asked how it would be done in PHP4, so it was reasonable to assume that is what you are using.

Nor 11-06-2007 01:22 AM

Quote:

Originally Posted by Andrew (Post 3655)
Well, how was I supposed to know that? You just asked how it would be done in PHP4, so it was reasonable to assume that is what you are using.

No I didn't, I said whats so great about the __construct(), only thing I mentioned about php4 was the __destruct() that they didn't really have..

Wildhoney 11-20-2007 12:32 AM

Destruct is called anyway when a script ends and so unsetting variables is quite unnecessary. Though many coders do like:

php Code:
unset($this);

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.

DragonBe 11-20-2007 01:03 AM

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

Morishani 11-20-2007 07:47 AM

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.

sketchMedia 11-26-2007 09:18 PM

Quote:

The reason the class-name-function was replaced by __construct() is to be more a genuine OOP language.
What about C++, that has its constructors like PHP4 as does Java, i.e. C++
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() {}
. (not sure about Java, something in my mind is saying finalize() but don't trust that as fact coz i ain't a Java programmer :), ill have to ask my m8 about that. )

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