I came across this problem. I have two object. One is just to create an array with words and the other one is for string actions. I instantiate an object that add words to an array. When I pass this array as an argument in foreach loop I get an error saying "strtolower expects parameter 1 to be string, array given". I spent some time thinking why is that and then I decided to hardcode the array and pass it as an argument in the same foreach loop. To my suprise it worked. I do not know what is happening.
<?php
class Words
{
private $words = [];
public function addWords(...$string)
{
$this->words[] = $string;
}
public function getWords()
{
return $this->words;
}
}
class StrAction
{
public function lowerCase($str)
{
if (is_array($str)) {
$array = [];
foreach ($str as $word) {
$newWord = strtolower($word);
$array[] = $newWord;
}
return $array ;
}else{
return strtolower($str);
}
}
}
$wordBank = new Words();
$wordBank->addWords('HELLO', 'Good Morning', 'alright mate');
$array = ['hello', 'good morning', 'alright mate'];
$strAction = new StrAction();
$strAction->lowerCase($wordBank->getWords());
// $strAction->lowerCase($array);
?>