Exact difference between self::__construct() and new self()

10,226

Solution 1

This is best illustrated in code:

class MyClass {

   public $arg;

   public function __construct ($arg = NULL) {
     if ($arg !== NULL) $this->arg = $arg;
     return $this->arg;
   }

   public function returnThisConstruct () {
     return $this->__construct();
   }

   public function returnSelfConstruct () {
     return self::__construct();
   }

   public function returnNewSelf () {
     return new self();
   }

}

$obj = new MyClass('Hello!');
var_dump($obj);
/*
  object(MyClass)#1 (1) {
    ["arg"]=>
    string(6) "Hello!"
  }
*/

var_dump($obj->returnThisConstruct());
/*
  string(6) "Hello!"
*/

var_dump($obj->returnNewSelf());
/*
  object(MyClass)#2 (1) {
    ["arg"]=>
    NULL
  }
*/

var_dump($obj->returnSelfConstruct());
/*
  string(6) "Hello!"
*/

return self::__construct() returns the value returned by the objects __construct method. It also runs the code in the constructor again. When called from the classes __construct method itself, returning self::__construct() will actually return the constructed class itself as the the method would normally do.

return new self(); returns a new instance of the calling object's class.

Solution 2

I believe that new self() would create a new instance of the class while self::__construct ()only calls the classes __construct method.

Share:
10,226
halfpastfour.am
Author by

halfpastfour.am

I'm a programmer and developer; currently Senior Developer at a company in Gelderland, The Netherlands. My territory: PHP: Laminas/Symfony JS: Node/Vue.js Python: Data science Machine learning, deep learning, AI

Updated on June 05, 2022

Comments

  • halfpastfour.am
    halfpastfour.am almost 2 years

    Can you tell me the exact difference between return self::__construct() and return new self()?

    It seems one can actually return a self::__construct() from a __construct() call when the object is created, returning the object itself as if the first __construct() was never even called.

  • gen_Eric
    gen_Eric about 12 years
    And __construct methods don't usually have return values.
  • N.B.
    N.B. about 12 years
    @Rocket - __construct always has a return value - the object created.
  • DaveRandom
    DaveRandom about 12 years
    @N.B. That's not the case, is it? Construct does not return the instance of the class, it is called during instantiation and returns NULL. In practice, at least.
  • gen_Eric
    gen_Eric about 12 years
    @N.B.: Not exactly. When doing new class, the return value of __construct is ignored, and the object is returned. But, when calling __construct like a normal function, it does just that. It acts like a function and returns whatever it returns. ideone.com/IZSi8
  • netcoder
    netcoder about 12 years
    __construct should always return void. It's the new operator that returns an instance of the class, not the constructor.
  • netcoder
    netcoder about 12 years
    It's also actually quite weird to call the constructor of an object after it has been constructed. Some languages doesn't even allow it. You probably just should separate the re-usable constructor logic in another method, e.g.: init().
  • halfpastfour.am
    halfpastfour.am about 12 years
    @netcoder One wouldn't normally call a constructor after the class has been constructed. It seems however possible to re-construct while the class is being constructed, something that may come in handy in the future.
  • Mehdi Karamosly
    Mehdi Karamosly over 10 years
    @DaveRandom I thought a php constructor is supposed to only return the object he creates, not a value.