5

I'm trying to create an array containing multiple objects.

I wrote this code (it's a member of an existing class)

public static $Roles = [
    (object) ['code' => 'SO', 'name' => 'Socio'],
    (object) ['code' => 'RESP', 'name' => 'Responsabile zona'],
    (object) ['code' => 'AMM', 'name' => 'Amministratore'],
];

but I get this error:

syntax error, unexpected '(object)' (object) (T_OBJECT_CAST), expecting ')'

on the second line.

I thought this should work, because I already used the same cast syntax to define associative array elements:

return view('edit-headquarter', [
  'hq' => (object)['name' => '', 'id' => 0],
  'submitAction' => 'insert'
]);

I'm doing something wrong?

EDIT: I'm using PHP 5.4.45

I'm not sure, but this can be related as suggested by Martin Persson

Community
  • 1
  • 1
Andrea Parodi
  • 5,534
  • 27
  • 46
  • I think this has something to do with your php version. If it doesn't work, you may think about declaring an Stdclass object or, even better, to implement a function that returns an stdclass object from the provided array. (also, another possible trick is to json_decode a json_encode(array)) – briosheje Mar 24 '16 at 10:19
  • @user340764 , where it works fine? https://3v4l.org/pXo2o – Federkun Mar 24 '16 at 10:22
  • What's the point? You convert arrays to `stdClass` objects which are just arrays with less features and a different syntax. They do not provide any useful OOP feature. You better create a class `Role` and pass the values of `code` and `name` as arguments to its constructor. There is no need for conversions any more and you can add more features to them as needed. – axiac Mar 24 '16 at 10:22
  • @axiac: I'm learning PHP, so I'm exploring possibilities. The point is to be able to use o::$Roles[0]->code in the same way I do with other object, just for consistency. I didn't understood why this isn't working, since I'm using same syntax in other place, as I said – Andrea Parodi Mar 24 '16 at 10:25
  • 2
    As the [documentation](http://php.net/manual/it/language.oop5.static.php) say "Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed." – Federkun Mar 24 '16 at 10:30
  • @AndreaParodi : for your task, I think the closest you can get is something like this: https://3v4l.org/nZtOE (mostly because you cannot use expressions in static declarations, as Federico said) – briosheje Mar 24 '16 at 10:35
  • @briosheje: thank you, I already refactored to declare the property uninitialized and then fill it later... – Andrea Parodi Mar 24 '16 at 10:37
  • I strongly recommend that you upgrade to PHP 7 – marpe Mar 24 '16 at 12:08

2 Answers2

3

If you're using PHP version below v5.6, then you will not be allowed to have an expression as a default value for class members. Other than that, I don't see anything wrong with the way you have declared it.

marpe
  • 198
  • 10
  • I extracted the expression to a variable, and used that variable as the default value. The object now creates just fine. I receive an error when defining the stating property. – Andrea Parodi Mar 24 '16 at 10:23
  • Ok, as suggested by Federico, I neither can use a variable as initializer. That make sense... Thank you all! – Andrea Parodi Mar 24 '16 at 10:33
0

To cast an associative array to object you can use a bit dirty, but widely used

$obj = json_decode(json_encode($arr));

LibertyPaul
  • 1,139
  • 8
  • 26