Question

In Yii What is the difference between the $model = new Users and $model = new Users() ?

Was it helpful?

Solution

Basically there is no difference between the two statements. you can use any statement to create an object. The difference plays its role when you want to pass the arguments to initialize the constructor. eg:- Lets consider the case of declaring the scenario. for that you will use

$model= new User('login');

This statement will bind the $model object with a scenario call 'login' which you can then use for validation purposes.
As you can see since you are not using () in case of $model = new Users; SO you cant create a scenario in this statement.

BUT this does not mean that you can never create a scenario now.
To create a scenario using $model = new Users; you need to write one more line i.e

$model->scenario='login';

So from all above $model= new User('login'); is equal to

$model = new Users;
$model->scenario='login';

Conclusion:- There is no difference.Both are same till the time you do not want to pass the argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top