Sokhrat Sikar
Sokhrat Sikar

Reputation: 175

returning values from function or method multiple times by only calling the class once

I have a members.php file that shows my websites members. I echo members name by using foreach method. A method of Members class returns an array, then I use foreach loop in the members.php file to echo the members. I am trying to aovid writing as less php code as possible in my members.php file where my html files are located. Is there a way to avoid using foreach inside members.php file?

For example, is it possible to return value from a method couple of times? (by only calling the object once). Just like how we normally call the functions? This question doesn't make sense, but I am just trying to see if there is a away around this issue?

Upvotes: 1

Views: 220

Answers (2)

Andreas Hagen
Andreas Hagen

Reputation: 2345

You could write an intermediate function that caches whatever the Members-method returns and return just one index from the cache, specified by a parameter. But then you are back to using a some kind of loop.

Upvotes: 1

Jacob S
Jacob S

Reputation: 53

There's nothing wrong with a loop in your view. After all, PHP is a templating language.

However, you could write an object method to return/print a formatted members list.

Upvotes: 0

Related Questions