TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-04-2007, 10:37 PM   #1 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default 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.
Nor is offline  
Reply With Quote
Old 11-05-2007, 04:08 AM   #2 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

__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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-05-2007, 05:13 AM   #3 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

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(){}
Nor is offline  
Reply With Quote
Old 11-05-2007, 10:19 PM   #4 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-05-2007, 11:12 PM   #5 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Quote:
Originally Posted by Andrew View Post
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.
Nor is offline  
Reply With Quote
Old 11-05-2007, 11:31 PM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Quote:
Originally Posted by Nor View Post
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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-06-2007, 01:22 AM   #7 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Quote:
Originally Posted by Andrew View Post
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..
Nor is offline  
Reply With Quote
Old 11-05-2007, 10:44 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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')); 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-20-2007, 12:32 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-20-2007, 01:03 AM   #10 (permalink)
The Wanderer
PHP Guru Advanced Programmer Zend Certified 
 
DragonBe's Avatar
 
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
DragonBe is on a distinguished road
Default

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
Send a message via ICQ to DragonBe Send a message via Skype™ to DragonBe
DragonBe is offline  
Reply With Quote
Old 11-20-2007, 07:47 AM   #11 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

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.
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-26-2007, 09:18 PM   #12 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 11-27-2007 at 06:42 AM.
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:02 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design