Click to See Complete Forum and Search --> : constructor


aopen
04-23-2007, 03:57 PM
Do you think constractor should be void method or should return something?

Thanks

NogDog
04-23-2007, 04:24 PM
My understanding is that it is meaningless to return anything, as when you instantiate an object via "new", it is "new" which is returning an object to the receiving variable, not the class's constructor. Therefore, any value returned by the constructor is essentially lost. It is probably better to set a class property, then if needed, check that property's value to determine the result of the object construction.

But, I am by no means a PHP5 OOP expert, so am willing to be convinced otherwise. :)

Roger Ramjet
04-24-2007, 08:21 AM
Although there is no 'rule' to this, a class constructor would not normally be used to return anything. Constructors are methods that are called every time a new object is created and so are used to initialise whatever an object will need before it can be used. Better to have it initialise a property or var and make that available through a get method than to have it return a value directly.

Weedpacket
04-24-2007, 10:31 PM
And if the reason for the "return value" is that it can't construct a legitimate object for some reason, the appropriate thing to do is to throw an exception.

Of course, all this is pretty much moot. Constructors don't return values:

<?php

class foo
{
function __construct($bar)
{
if($bar!=42) throw new Exception("FORTY-TWO");
return 'Fnord';
}
}

try
{
$t = new foo(42);
echo get_class($t),"\n";
}
catch(Exception $e)
{
echo "Couldn't create \$t - the constructor said: ",$e->getMessage(),"\n";
}

try
{
$u = new foo(17);
echo get_class($u),"\n";
}
catch(Exception $e)
{
echo "Couldn't create \$u - the constructor said: ",$e->getMessage(),"\n";
}

$booboo = foo();
echo $booboo;

Roger Ramjet
05-02-2007, 08:38 AM
Of course, all this is pretty much moot. Constructors don't return values:

And that is an even better reason.

Had not actually realised that as I've never tried to do it. Makes sense when you think about it.

Shrike
05-04-2007, 06:56 PM
I suppose it's worth noting that constructors are regular class methods that just happen to be called __construct(). So in a sense they can return values, but when called by the PHP engine during object creation (a.k.a. 'new' ) the return value is ignored.

Shrike
05-04-2007, 07:15 PM
Pointless tests follow.

class A
{
public static function __construct()
{
return 1;
}
}
A::__construct();
// Fatal error: Constructor A::__construct() cannot be static

So they aren't exactly regular methods. So lets remove the static keyword.

class A
{
public function __construct()
{
return 1;
}
}
A::__construct();
// Apache crash

The above crashes Apache 2 with PHP 5.2.1 on Windows XP. Neat. Anyone else care to test?

class A
{
public function __construct()
{
return 1;
}
}
$a = new A;
echo $a->__construct();
// 1

Hurrah.

rvdavid
05-07-2007, 03:52 AM
Do you think constractor should be void method or should return something?

Thanks

I agree with the general consensus. You could look at the constructor as a special method for a class that returns an object instance - this is done in the background, you don't need to worry about it.

Regards,

Weedpacket
05-07-2007, 07:08 AM
Hurrah.And there was much rejoicing....