php - Why constructors can't return a value other than object even if it is explicitly instructed to do so? -


i noticed constructors in php classes cannot return value other objects when implicitly called outside of class:

class { function __construct () {    return false; }  function afunctioninside () {  $avar = $this->__construct ();  echo gettype($avar);  } }  $a = new a; $a->afunctioninside (); // boolean echo gettype ($a); //object 

is behavior helpful in case?

a constructor implicitly called when object created (with new). however, return value not used in case, , has nothing object being created.

it not constructor creates object, because without one, object created anyway. what's more, when constructor runs, object there.

no return value expected constructor. according the manual has no return type:

constructor

void __construct ([ mixed $args = "" [, $... ]] ) 

you of course free return , use when call function explicitly, other function, don't expect call create instantiation of class. need use new syntax.

the return value plays no role when called implicitly during object creation: return value ignored in case.


Comments