Skip to content

Inherited __clone is not called #57

Description

@quasilyte
<?php

class Foo {
  private final function __clone() {
    var_dump(__METHOD__);
  }
}

class Bar extends Foo {
}

$x = new Bar();
$_ = clone $x;

PHP: runtime error: Call to private Foo::__clone()
KPHP: no errors, default (?) cloning is performed

The final private __clone is a trick to make objects non-cloneable.

Note: even if we make the __clone() public (final is irrelevant), it's not going to be called from the derived objects.

<?php

class Foo {
  public function __clone() {
    var_dump(__METHOD__);
  }
}

class Bar extends Foo {}
class Baz extends Bar {}

$x = new Baz();
$_ = clone $x;

PHP: prints "Foo::__clone"
KPHP: prints nothing

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions