public static function copy( $input, bool $deep = true ) {

    if( !static::is( $input ) ) return $input;
    else {

      if( is_array( $input ) ) {

        $result = [];
        foreach( $input as $k => $e ) {
          $result[ $k ] = $deep ? static::copy( $e, $deep ) : $e;
        }

      } else {

        // handle (and detect non-)cloneable object (private __clone method is considered non-cloneable)
        if( method_exists( $input, '__clone' ) && !is_callable( [ $input, '__clone' ] ) ) $result = $input;
        else if( !( $input instanceof \StdClass ) ) $result = clone $input;
        else {

          $result = new \stdClass();
          foreach( $input as $k => $e ) {
            $result->{$k} = $deep ? static::copy( $e, $deep ) : $e;
          }
        }
      }

      return $result ?? $input;
    }
  }
public static function copy( $input, bool $deep = true ) {

    if( !static::is( $input ) ) return $input;
    else {

      if( is_array( $input ) ) {

        $result = [];
        foreach( $input as $k => $e ) {
          $result[ $k ] = $deep ? static::copy( $e, $deep ) : $e;
        }

      } else {

        // handle (and detect non-)cloneable object (private __clone method is considered non-cloneable)
        if( method_exists( $input, '__clone' ) && !is_callable( [ $input, '__clone' ] ) ) $result = $input;
        else if( !( $input instanceof \StdClass ) ) $result = clone $input;
        else {

          $result = new \stdClass();
          foreach( $input as $k => $e ) {
            $result->{$k} = $deep ? static::copy( $e, $deep ) : $e;
          }
        }
      }

      return $result ?? $input;
    }
  }
public static function copy ( $ input , bool $ deep = true ) { if ( ! static :: is ( $ input ) ) return $ input ; else { if ( is_array ( $ input ) ) { $ result = [ ] ; foreach ( $ input as $ k => $ e ) { $ result [ $ k ] = $ deep ? static :: copy ( $ e , $ deep ) : $ e ; } } else { // handle (and detect non-)cloneable object (private __clone method is considered non-cloneable) if ( method_exists ( $ input , '__clone' ) && ! is_callable ( [ $ input , '__clone' ] ) ) $ result = $ input ; else if ( ! ( $ input instanceof \ StdClass ) ) $ result = clone $ input ; else { $ result = new \ stdClass ( ) ; foreach ( $ input as $ k => $ e ) { $ result -> { $ k } = $ deep ? static :: copy ( $ e , $ deep ) : $ e ; } } } return $ result ?? $ input ; } }
Deep copy of a collection

@param array|object $input
@param bool         $deep Deep copy or not

@return array|object
Deep copy of a collection