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

4
  • 1
    blog.programster.org/php-creating-strict-type-arrays Commented May 6, 2021 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. Commented May 6, 2021 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; Commented May 6, 2021 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. Commented May 6, 2021 at 18:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.