JimK
JimK

Reputation: 19

New php syntax (for me)

I have some experience with php, but I just saw a syntax (Yii2 reference) I'm not sure how to read and couldn't find explicitly in the docs. What does the "[new" mean? Does this define an array of new objects?

public function actionCreate()
{
    $model = new Credit();
    $modelReferences = [new CreditRefence()];
    $modelFiles = [new CreditFile()];

Upvotes: 0

Views: 57

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133380

The new is for create a new object/model in this case you are creating 3 object Credit, CreditRefence, CreditFile

the first is assingne $model a simple var

the other are assigned to array .. [] is the new shortcut sintax for array in php 5.4

In your application probably you have Credit, CreditRefence, CreditFile in model

Upvotes: 0

Kevin_Kinsey
Kevin_Kinsey

Reputation: 2300

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

Source: http://php.net/manual/en/language.types.array.php

Upvotes: 1

Related Questions