0

I have a class that extends ArrayObject

class Collection extends ArrayObject

I know i can define array of objects using this code:

/* @var $userArray Model_User[] */

But how can i define variable $userArray as an custom array of class Collection that contains objects of class Model_User?

Without changing class Collection or its phpdoc. I want to use the same class Collection for different arrays of objects.

This is not the same as PHPDoc type hinting for array of objects?, because in that subject discussion is related to common arrays in php, which are phpdoced as /* @var $userArray Model_User[] */ in the mean time my question relates to custom build array which if phpdoced my method above will not type hint methods of custom build array class, like so $userArray->echoChanges() because it will think its a common array, not an array of class Collection. And yes $userArray also acts as an array, so it should type hint array contents $userArray[3]->name.

4
  • Possible duplicate of PHPDoc type hinting for array of objects? Commented Sep 7, 2016 at 17:36
  • not even close, my array can have its own functions, like $userArray->resetChanges() so it should type hint + its also acts as an array $userArray[4] ->name and it should type hint as well. Commented Sep 8, 2016 at 5:31
  • In other words what your asking is: where $x = [new Name()]; then /* @var $x Name[] */ so $y = new Collection([new Name()]); then /* @var $y ? */ Commented Sep 8, 2016 at 6:27
  • code will work like this: $a = new Collection(); $a[] = new Model_User(); $a->resetChanges(); $a[0]->name = "George"; $a->echoChanges(); and i dont know how to phpdoc $a, so its knew it has methods of class Collection and contains objects of class Model_User. just in case php.net/manual/ru/class.arrayobject.php Commented Sep 8, 2016 at 6:41

1 Answer 1

1

PSR-5: PHPDoc proposes a form of Generics-style notation.

Syntax

Type[]
Type<Type>
Type<Type[, Type]...>
Type<Type[|Type]...>

Values in a Collection MAY even be another array and even another Collection.

Type<Type<Type>>
Type<Type<Type[, Type]...>>
Type<Type<Type[|Type]...>>

Examples

<?php

$x = [new Name()];
/* @var $x Name[] */

$y = new Collection([new Name()]);
/* @var $y Collection<Name> */

$a = new Collection(); 
$a[] = new Model_User(); 
$a->resetChanges(); 
$a[0]->name = "George"; 
$a->echoChanges();
/* @var $a Collection<Model_User> */

Note: If you are expecting an IDE to do code assist then it's another question about if the IDE supports PHPDoc Generic-style collections notation.

Sign up to request clarification or add additional context in comments.

2 Comments

thx, thats what i was looking for, as for type hinting if anyone needs stackoverflow.com/questions/10706835/…. And also PSR-5 is in a draft status, so it may change.
In this exemple, what PHPDoc do you apply to Collection and on the ArrayAccess' methods ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.