PHP 5 deprecated functions for migrating to PHP 7, function @

Asked

Viewed 86 times

2

I’m having to migrate an entire project done in PHP 5 to PHP 7, when running a diagnostic tool, it points out that some functions use the @ and are depreciated for example:

/**
 * Creates a copy of a class object
 * @param $object (object) class object to be cloned
 * @return cloned object
 * @since 4.5.029 (2009-03-19)
 * @public static
 */
public static function objclone($object) {
    if (($object instanceof Imagick) AND (version_compare(phpversion('imagick'), '3.0.1') !== 1)) {
        // on the versions after 3.0.1 the clone() method was deprecated in favour of clone keyword
        return @$object->clone();
    }
    return @clone($object);
}

Note that the @ to deal with it. I need to change the source safely so the system continues to function normally, as I can deal with the cases that last the @?

  • At some point the imagick in those old versions?

  • Yes, it is still used. clone() of a non-existent object is missing error

  • errata *fatal error

1 answer

0


The operator @ is used to suppress error messages. It may even be that the code that depends on this method is already wrong in time, even in PHP 5.

In that case I would remove the @ to ensure that everything is working. It is possible to include other type checks in order to ensure that when arriving at the clone no major problems.

You can for example add a hinting type of object to ensure that only objects will be passed to this method (works from PHP 7.2):

public static function objclone(object $object) {

}

In this case when using an invalid type you will know where you need to store.

Another less disruptive approach is to check the type of the variable and if it is not an object return the variable itself, because the clone only works with objects and return a primitive variable is similar to clone behavior (in PHP only objects receive a reference in assignment):

public static function objclone($object) {
    if (!is_object($object)) {
        return $object;
    }

    // ...
}

Another option that can give more work, check if you really need to make a clone of the objects. In addition to improving memory consumption, your code will be more readable. And if you need to use directly clone instead of calling the function.

Last but not least, confirm the correct version of Imagick. According to documentation of that method, it was discontinued in version 3.1.0, not in version 3.0.1.

Try changing your code to use this version. In a compiled version, your method can look like this:

public static function objclone($object) {
    if (!is_object($object)) {
        // If is not an object, return the variable itself
        return $object;
    }

    if (($object instanceof Imagick) && (version_compare(phpversion('imagick'), '3.1.0') !== 1)) {
       // clone() method was deprecated in favor of clone keyword
       // see https://www.php.net/manual/en/imagick.clone.php
       return $object->clone();
    }

    return clone $object;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.