Do you think constractor should be void method or should return something?
ThanksMy 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. 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.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;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.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.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.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,Hurrah.And there was much rejoicing....
ThanksMy 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. 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.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;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.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.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.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,Hurrah.And there was much rejoicing....