简体   繁体   中英

How to write that in PHP 7.2?

I have this piece of code:

private function _resolveCustomEntries($fields)
{
    foreach ($fields as &$value) {
        if (array_key_exists('custom', $value)) {
            if (method_exists($this, $value['custom'])) {
                $value['custom'] = $this->$value['custom'](); //variableInterpolation
            }
        }
    }
    return $fields;
}

I ran a PHP 7.2 compatibility check and it complained here with the "variableInterpolation" on the marked line. When I run this code, the PHP log tells me this:

ERR (3): Notice: Array to string conversion in /public_html/lib/KiTT/Payment/Widget.php on line 217

Thats the same line where the "variableInterpolation" check failed. So how would I rewrite this code so it works in PHP 7.2?

Thanks!

Solution:

$value['custom'] = $this->$value['custom']();

has to look like this:

$value['custom'] = $this->{$value['custom']}(); 

It's a matter of order variables are evaled.

With

class x {
  public function y() {
    echo 'ok';
  }
}
$x = new x();
$y = array('i' => 'y');

Then

$x->$y['i']();

Fails because PHP first tries to cast the $y variable into a string, and get the matching property of $x (which btw does not exist), then tries to get the index 'i' or that unexisting property, and then tries to run it as a callable.

Hence 3 errors:

Array to string conversion

Undefined property x::$Array

Function name must be a string (nda: the undefined property returns NULL)

Instead, curly brace the variable to set the resolving order:

$x->$y['i']();

Will work. So use $this->{$value['custom']}()

This will throw an array to string conversion in 7.2

class bob{


    function foo(){
        return 'bar';
    }

    function getFoo(){

        $value['custom'] = 'foo';

        $value['custom'] = $this->$value['custom']();

        return $value['custom'];
    }


}

$bob = new Bob();

var_dump($bob->getFoo());

But it will execute just fine in 5.6.

Then i changed the snippet to this, not calling the method directly casting the array key to function name, but initializing a string (hopefully, there is no type validation in your code) variable with the function name first:

class bob{


    function foo(){
        return 'bar';
    }

    function getFoo(){

        $value['custom'] = 'foo';

        $functionName = $value['custom'];

        $value['custom'] = $this->$functionName();

        return $value['custom'];
    }


}

$bob = new Bob();

var_dump($bob->getFoo());

This will run just fine in php 7.2

You could try rewriting your code using complex (curly) syntax, you can read more about it here .

Your code would look something like this.

$value['custom'] = $this->{$value['custom']}();

Edit: moved the curly braces to correct positions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM