O Connor
O Connor

Reputation: 4402

Calling a method that returns a value each time or store it in a variable in PHP and MVC

In general, we pass a model through the controller to the view when we work with MVC. In case the model has a method that returns a certain value which will be needed several times on the same view. Which is a better practice between calling that method each time we need it's returned value or call it once and store the returned value in a variable and use that variable each time we need it? And why?

Upvotes: 1

Views: 98

Answers (1)

luklapp
luklapp

Reputation: 205

In my opinion it's better to call that method once and store it into a variable. Imagine that this method contains a complex calculation, an API call or something like that. In this cases it would be much faster to calculate/execute it once and to store it in a variable.

Upvotes: 1

Related Questions