create object of the same class that passed object in php
class, object, oop, php
Solution
May be able to simplify it, but `get_class()` is the way to go:
$class = get_class($obj);
$newObj = new $class;
I couldn't find a one-liner for PHP 5.x or 7.x, but this appears to work in PHP 8.0:
$newObj = new (get_class($obj));
Problem
I have the fallowing situation ``` class A { ... public static function Copy($obj) { $newObj = new (class of $obj); ... } } class B extends A { ... } class C extends A { ... } ... $newB = B::Copy($BObject); $newC = C::Copy($CObject); ``` Can I create a new object of the parameters class, or I have to override method for every inherited class?