0

What is the proper syntax to declare a type string[] attribute in php?

protected string[] $array_of_strings;

does gives an error at '['

theking2
  • 2,174
  • 1
  • 27
  • 36
  • 2
    PHP doesn’t have such syntax. You can use `@return string[]` in a docblock if you need an IDE to pick it up – Clive Mar 10 '22 at 19:05
  • 1
    @Clive In this case, it's a property, not a function return type, so it would be `@var string[]` – IMSoP Mar 10 '22 at 19:36
  • Side note: this is a "[property](https://www.php.net/manual/en/language.oop5.properties.php)"; in PHP 8, there is a completely separate thing called an "[attribute](https://www.php.net/manual/en/language.attributes.overview.php)", so you may confuse people if you use the wrong term. – IMSoP Mar 10 '22 at 19:44
  • 1
    Bruh I was finishing my answer when this got closed! you can only do `protected array $array_of_strings;` https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.typed-properties https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.base – medilies Mar 10 '22 at 19:44

1 Answers1

-2

You can also use like:

$Array = ["foo","bar", "value"];