On 2025-07-04 19:38, Alexandru Pătrănescu wrote:

I'm thinking about constructors usually like this:
They are functions that are invoked statically (using the `new` keyword) on the class before the object is created, and they execute on instance level, after the object is created.

They are called as static methods, and they are very similar to static factory methods, and for better or worse, static methods are supported in interfaces.

They're really more instance methods (they need an instance to exist for them to act on, hence $this). The "new" operator creates the instance and then the __construct method is called on that new instance.

One consequence of this is that the __construct method can be called _after_ instantiation (because in the normal course of things it _is_):


class Foo
{
        public function __construct(public int $v=0)
        {
                echo "Constructed {$this->v}!";
        }
}

$t = Foo::__construct(5);


$u = new Foo(17);
$u->__construct(42);

Reply via email to