A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

PHP classes, __construct

957 views · started by GAMEchief ·
#1
PHP classes, __construct
So I see __construct being used in the PHP sources. I've recently picked up on using OOP in PHP, and I've been doing
class Whatever {
function Whatever() { /* shit to do on call */ }
}

What is the difference? Is one processed faster? Or what?
#2
__cunstruct is executed on the creation of the class.

Example;

$Class = new Class($Variable);

That input would be used for the __cunstruct function.

__destruct happens when the PHP processing is about to output the final web page.

I use it to either display errors, or unset all variables.
#3
If you name a function the same as the class name, it executes on the creation of the class as well.

e.g.

class test {
function __construct() { echo 'hello, world'; }
}

and
class test {
function test() { echo 'hello, world'; }
}

are synonymous.
I was just wondering if one was faster than the other, or if one was standard, or what.
#4
GAMEchief wrote:
If you name a function the same as the class name, it executes on the creation of the class as well.

e.g.

class test {
function __construct() { echo 'hello, world'; }
}

and
class test {
function test() { echo 'hello, world'; }
}

are synonymous.
I was just wondering if one was faster than the other, or if one was standard, or what.


Um...neither is faster __construct is just meant to be a set-up type of method.

I think it is more of a "Proper" method.

For that much I don't think having a function the same name as the class is proper, but it is all about preference I guess.