0

I have a class where I want to set an array to have a strict type of class in php , eg.

class Person{
public string $name;
public array:Person $friends;
}

how do I do this

  • 1
    https://blog.programster.org/php-creating-strict-type-arrays – Kaii May 06 '21 at 18:23
  • i could write an answer, but this post is so much better and i couldn't find a suitable duplicate to answer your question. – Kaii May 06 '21 at 18:25
  • TL;DR: you can't, but you can create your own implementation of an `ArrayObject` which behaves like an array. `public PersonCollection $friends`; – Kaii May 06 '21 at 18:26
  • Depending on exactly _why_ you want this, the answer might be "you can't", "you can build it yourself with a fair amount of effort", or "you can put it in the docblock and use an IDE or command-line tool to verify it". I haven't read through all of the linked answers, but I imagine they cover most of the options between them. – IMSoP May 06 '21 at 18:35

0 Answers0