-1

I've been reading some code recently and I've come across a few instances where the developer would wrap parentheses around like following example.

There's this function:

foreach ((array) $middleware as $m) {
    $this->middleware[] = [
    'middleware' => $m,
            'options' => &$options,
    ];
}

In the for each check there's array (array) with parentheses around, what do these mean.

Or can someone guide me to a doc where I can read up on this matter. Thanks in advance.

ArneDB
  • 33
  • 6
  • Thanks for the quick answers guys, you helped me a lot. To bad it was a duplicate, I was coming at this from another angle. I didn't know the right name for it, so I didn't know how to search for it. The guy in the "duplicate" post actually knew what he was searching for, he just didn't know how to do it :P – ArneDB Nov 28 '18 at 13:51

1 Answers1

5

This is called type casting.

It forces $middleware to an array data type. Some examples:

(array) [1,2] // [1,2]
(array) 1 // [1]
(array) stdClass{hello: world} // [hello => world]  (object to array pseudocode)
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95