Entirely depends on context. The preferred solution would be one of two things (or possibly a combination of both):
- If your array is a set of values all having consistent type, specify that sub-type explicitly. For instance, if you have a
$param array of
\My\Class\Model\Something instances, rather than
array $param, your type definition should be
- If your array is not a set of consistent type, but always follows the same pattern (it should!), convert it into an object with a defined interface.
For #2: Say you have an array to hold multiple values for the response:
$response = [
'message' => 'Success',
'error' => false,
Rather than returning this array (type
array), you should turn that into an object:
class Response extends \Magento\Framework\DataObject implements ResponseInterface
public function setMessage(string $message)
return $this->setData('message', $message);
public function getMessage()
public function setError(bool $error)
return $this->setData('error', $error);
public function getError()
And then use dependency injection to inject a
$response = $this->responseFactory->create();
And suddenly you have a defined interface, and every bit of code knows that response is going to be an instance of
Response, with a message and an error flag, as well as the types of each, and how to get to them.
By defining types, you remove ambiguity and the possibility of anomalous behavior.