Deprecated - str_replace() - Passing null to parameter #3 ($subject)

Deprecated - str_replace() - Passing null to parameter #3 ($subject)

DSC Web DesignDSC Web Design Posts: 3Questions: 0Answers: 0

Hello Allan and Community

I would like to report another PHP deprecated warning related to null being passed on to a function.

We have installed DataTables version 2.3.4 and Editor version 2.5.0. PHP is currently set to version 8.1 but it is intended to upgrade it to a higher version.

The tables were programmed quite a while ago and did not have this issue while they were running on PHP 7. But we have done a server upgrade, with up-to-date Linux, and running PHP 7 is no option anymore; PHP 8 must now be used.

This is the PHP deprecated warning that occurs:

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/.../lib_2.5.0/Editor/Format.php on line 261

This is the function where it occurs:

public static function toDecimalChar($char = ',') {
  return static function ($val, $data) use ($char) {
    return str_replace('.', $char, $val);
  };
}

It is a major undertaking to go through all the tables and make adjustment for this problem in the tables themselves.

As a quick fix I decided to edit the affected section in the Format.php file itself. Here is a possible workaround for this problem:

public static function toDecimalChar($char = ',') {
  return static function ($val, $data) use ($char) {
    if (isset($val) && $val != null)
      return str_replace('.', $char, $val);

    else
      return null;
  };
}

I hope this posting will be helpful to fix these small issues and make this great software more compatible with future PHP versions.

Sincerely,
Gevorg

Replies

  • allanallan Posts: 65,254Questions: 1Answers: 10,816 Site admin

    Hi Gevorg,

    Many thanks for your message. Can you show me the setup for how the Field is configured here please? Are you using ifEmpty(null) and a formatter, or something else?

    Allan

  • DSC Web DesignDSC Web Design Posts: 3Questions: 0Answers: 0

    Hello Allan.

    Always prompt with your replies, I don't know how you do this, maybe you work as a group :-)

    Anyway...

    It has been a while since I worked on these files, but I will try to answer your question.

    Indeed what you mention is being used. Here is one example of how these tables were programmed many years ago:

    Field::inst( 'pb_OBFUSCATED-FOR-POSTING.idnr' )
      ->validator( Validate::numeric(
      /* The first parameter to be passed into the
       * Validate::numeric() function should be the decimal
       * place character, not a ValidateOptions instance,
       * in this case: ''
      */
      '',
      ValidateOptions::inst()
        ->message( 'Nur Ziffern sind erlaubt.' )
      ) )
      ->validator( function ( $val, $data, $field ) {
        return strlen( $val ) < 1 ?
        'Mindestens 1 Ziffer nötig.' :
         true;
      } )
      ->validator( function ( $val, $data, $field ) {
        return strlen( $val ) > 5 ?
        'Maximal 5 Ziffern erlaubt.' :
        true;
       } )
    
      ->setFormatter( Format::ifEmpty(null) ),
    

    At the end you have the

    ifEmpty(null)

    which you mention.

    Is this way of setting up the fields no longer recommended with the current versions of DataTables / Editor ?

  • allanallan Posts: 65,254Questions: 1Answers: 10,816 Site admin

    Hi,

    Yes, that looks like a spot on way of doing it. I've committed a fix to address this problem and it will be in the next release of Editor. I'll look at getting that out fairly soon - perhaps next week if not tomorrow as there are a couple of little things that need to get pushed out :).

    Allan

    p.s. Wish I was always this quick!

  • DSC Web DesignDSC Web Design Posts: 3Questions: 0Answers: 0

    Thank you.

Sign In or Register to comment.