Type Hinting - Specify an Array of Objects

Type hinting in PHP 7 - array of objects

It's not included.

If it's not included, do you have Any clue why it was not included when type hinting was added?

With the current array implementation, it would require checking all array elements at runtime, because the array itself contains no type information.

It has actually already been proposed for PHP 5.6 but rejected: RFC "arrayof" - interestingly not because of performance issues which turned out to be neglible, but because there was no agreement in how exactly it should be implemented. There was also the objection that it is incomplete without scalar type hints. If you are interested in the whole discussion, read it in the mailing list archive.

IMHO array type hints would provide most benefit together with typed arrays, and I'd love to see them implemented.

So maybe it's about time for a new RFC and to reopen this discussion.


Partial Workaround:

you can type hint variadic arguments and thus write the signature as

function findUserByAge(int $age, User ...$users) : array

Usage:


findUserByAge(15, ...$userInput);

In this call, the argument $userInput will be "unpacked" into single variables, and in the method itself "packed" back into an array $users. Each item is validated to be of type User. $userInput can also be an iterator, it will be converted to an array.

Unfortunately there is no similar workaround for return types, and you can only use it for the last argument.

Is there a type hint for an array of objects of a specific class in PHP 8.1?

The short answer is no.

My understanding is that enforcing such a thing has terrible performance problems. Some people want to go all the way and have full blown generics, others just want type-safe collections. Some people worry that implementing the latter first might hinder the development of the former in the future. So we’re at a standstill.

There’s the occasional discussion in the community such as this: https://externals.io/message/108175

PHP type hinting array of objects

One option (requires PHP >= 5.6.0) is to define the method as

public function __construct(Toggleable ...$devices)

but you'll have to use array packing/unpacking on both sides; the constructor and wherever you instantiate the object, e.g.

$toggleAbles = [new Toggleable(), new Toggleable()];
$toggler = new Toggler(...$toggleAbles);

What's the type hint for an array?

As Carcigenicate pointed out, you can use array.array directly as a type annotation. However, you can't use array.array[int] or array.array[float] to specify the type of the elements.

If you need to do this, my suggestion is to use the MutableSequence generic type from the typing module, since arrays implement all of the necessary operations: append, extend, pop, remove, etc.

from typing import MutableSequence

arr: MutableSequence[float] = array.array('f')

In Python 3.9+, you can import it from collections.abc instead.

Type hinting - specify an array of objects

If you want to ensure you are working with "Array of Foo" and you want to ensure methods receive "Array of Foo", you can:

class ArrayOfFoo extends \ArrayObject {
public function offsetSet($key, $val) {
if ($val instanceof Foo) {
return parent::offsetSet($key, $val);
}
throw new \InvalidArgumentException('Value must be a Foo');
}
}

then:

function workWithFoo(ArrayOfFoo $foos) {
foreach ($foos as $foo) {
// etc.
}
}

$foos = new ArrayOfFoos();
$foos[] = new Foo();
workWithFoo($foos);

The secret sauce is that you're defining a new "type" of "array of foo", then passing that "type" around using type hinting protection.


The Haldayne library handles the boilerplate for membership requirement checks if you don't want to roll your own:

class ArrayOfFoo extends \Haldayne\Boost\MapOfObjects {
protected function allowed($value) { return $value instanceof Foo; }
}

(Full-disclosure, I'm the author of Haldayne.)


Historical note: the Array Of RFC proposed this feature back in 2014. The RFC was declined with 4 yay and 16 nay. The concept recently reappeared on the internals list, but the complaints have been much the same as levied against the original RFC: adding this check would significantly affect performance.

Function return type hinting for an array of objects in PHP7

I actually understand what you mean, but the answer unfortunately is that you can't do that. PHP7 lacks that kind of expressivity, so you can either declare your function to return "array" (a generic array) or you have to create a new class ItemArray which is an array of Item (but that meaning you will have to code it yourself).

There is currently no way to express "I want an array of Item" instances.

EDIT: As an added reference, here the "array of" RFC of what you wanted to do, it has been declined due to various reasons.

PHPDoc type hinting for array of objects?

Use:

/* @var $objs Test[] */
foreach ($objs as $obj) {
// Typehinting will occur after typing $obj->
}

when typehinting inline variables, and

class A {
/** @var Test[] */
private $items;
}

for class properties.

Previous answer from '09 when PHPDoc (and IDEs like Zend Studio and Netbeans) didn't have that option:

The best you can do is say,

foreach ($Objs as $Obj)
{
/* @var $Obj Test */
// You should be able to get hinting after the preceding line if you type $Obj->
}

I do that a lot in Zend Studio. Don't know about other editors, but it ought to work.

type hinting an array

Most of the standard library is not type annotated. mypy is using the stubs for the standard library from the typeshed project (which along with the standard library, also contains annotations for popular third-party library as provided by various contributers). For the array module, you can see that it is type annotated as generic:

import sys
from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, Tuple, TypeVar, Union, overload
from typing_extensions import Literal

_IntTypeCode = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
_FloatTypeCode = Literal["f", "d"]
_UnicodeTypeCode = Literal["u"]
_TypeCode = Union[_IntTypeCode, _FloatTypeCode, _UnicodeTypeCode]

_T = TypeVar("_T", int, float, str)

typecodes: str

class array(MutableSequence[_T], Generic[_T]):
typecode: _TypeCode
itemsize: int
@overload
def __init__(self: array[int], typecode: _IntTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
@overload
def __init__(self: array[float], typecode: _FloatTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
@overload
def __init__(self: array[str], typecode: _UnicodeTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
@overload
def __init__(self, typecode: str, __initializer: bytes | Iterable[_T] = ...) -> None: ...
def append(self, __v: _T) -> None: ...

...

The solution is to use MutableSequence as noted as answered in the question you linked to. Note, since Python 3.9+, typing.MutableSequence (along with things like typing.List and typing.Dict) have been deprecated, and the types themselves support generics, so use import collections and collections.abc.MutableSequence

Typehinting array of models

No, you can't do this. The best you can do is to typehint the parameter as an array. You can read more about type-hinting in PHP here: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

UPDATE
In PHP 5.6+ you can use variable length argument lists to achieve what you want. Check the answer below



Related Topics



Leave a reply



Submit