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

8
  • 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)) Commented Mar 24, 2016 at 10:19
  • @user340764 , where it works fine? 3v4l.org/pXo2o Commented Mar 24, 2016 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. Commented Mar 24, 2016 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 Commented Mar 24, 2016 at 10:25
  • 2
    As the documentation say "Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed." Commented Mar 24, 2016 at 10:30

2 Answers 2

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.

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

2 Comments

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.
Ok, as suggested by Federico, I neither can use a variable as initializer. That make sense... Thank you all!
0

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

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

Comments

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.