How to extract the $parameters array from the __callStatic() method for further conversion to a string using the __ToString () method?

Viewed 29
class Concatenator
{
    private static $string = [];

    public static function __callStatic($method, $parameters)
    {   
        self::$string = $parameters;
    }

    public function __toString()
    {
        return str_replace(' ', '+', strtolower(self::$string));
    }
}

$concatenated = Concatenator::prepareString('I am concatenated');

echo $concatenated;

I can't figure out how to pass the value from the magic __callStatic() method to the magic __toString().

Please tell me what is wrong with the code? Why doesn't __toString() see self::$string?

1 Answers

$string is an array, not a String:

private static $string = [];

so you cannot pass it to strtolower by itself. You might want to change its initialization to String:

private static $string = "";

and to pass $parameters as a String value instead of an array. Or, if it is correct to have an array, then you will need to use its proper index.

Related