Reputation: 1
My application uses classes consisting of functions, functions normally use "return $var" with a result of their work. I am wondering, if instead of "return" I will echo the result (an array) directly from within the function, is it acceptable? Just in some case it is more convenient for me. Thanks.
Upvotes: 0
Views: 172
Reputation: 8826
There is nothing bad about echoing some output by a method of a class, unless it's against design pattern you use and if you develop non-trivial application you should use one.
The most popular is so called Model-View-Controller (MVC) pattern, where the output should be echoed only by selected methods of View classes. Methods of other classes should care only about their bussiness and let the View render all output. But this is one of the design patterns and you don't have to use it.
Upvotes: 0
Reputation: 5883
Well, echoing an array will result in an output of the string Array()
in php.
If you meant: echoing the results of that array from within the function, it is acceptable but really not recommended: theoretically functions should have nothing to do with output. You should give them values to have them processed and give you back the result of that processing - if you don't need the returned values for more processing, why would you need functions at all, at least in the context of complex web applications?
This was the part strictly concerning your question.
There is a more general point to make, though, and that is: presentation (what the users see) should be separated from procedures.
The easiest way to achieve this is simply calling your functions (with data from $_GET, $_POST or the url) from your html/php pages; if you want to learn a cool approach that will be very useful for you in the future, check out the MVC pattern and some MVC frameworks.
Upvotes: 0
Reputation: 3137
It is acceptable - but it may reduce the flexibility and portability of your classes; what happens if you or somebody else wants to get your array of results and act upon it (or some of the key / vals) before it's displayed?
Always think about portability - if there's a chance your function may be used in a way other than the one way you're using it at the time you write it, make it as portable as possible.
Upvotes: 0
Reputation: 54425
There's absolutely nothing wrong with having some functions/methods that echo and others that return - in fact I'd expect the bulk of web applications to use a mix of these.
However, I'd consider the following:
You'll probably want to keep the number of "output" functions (i.e.: functions that echo) to a minimum so that there are less areas that need to be checked for cross site scripting issues and correct entity handling, etc.
You should always try and separate the calculation or "doing" part of the functionality from the output (a separation of concerns at a very basic level it you will).
Upvotes: 0
Reputation: 14746
It is better to separate the "logic" part of your application, and the "view" part of it.
I think it's better to leave the "return" on your methods and echo the results in the "font-end" part of your website.
P.s. take a look at MVC design pattern :) It's really good to understand how to logically divide your application.
Upvotes: 4