3

I'm building my personal Database connection model and one thing that I needed to do is allow the user to start a loop using a function and end the loop in another function. This is the code:

Code for running the functions

PSMQuery::loopQueryStart("DATA");
  // Stuff that can be executed in the loop
PSMQuery::loopQueryStop();

Code in the class:

public function loopQueryStart($query) {
  while (CONDITION):
}

public function loopQueryEnd() {
  endwhile;
}

And I was wondering if this method should work and if there would be another way, just to simplify the look of the code for someone using my model.

6
  • 1
    what the hell are you trying to accomplish? Commented Apr 9, 2016 at 0:05
  • @Federico a loop being started with a function, the in the middle the user can use their input then another function which will close the loop. - kind of like the CodeIgniter method for Auth Commented Apr 9, 2016 at 0:06
  • 1
    This makes no sense to me. Can't you just pass a function/reference to, eg PSMQuery::loop("Data", function($var) { /* sth */})? Commented Apr 9, 2016 at 0:10
  • You can't do it. Blocks have to be properly nested. Commented Apr 9, 2016 at 0:11
  • Why wouldn't you have your loopQueryEnd function make the CONDITION false, thus ending the while loop? Commented Apr 9, 2016 at 0:37

1 Answer 1

2

As I stated in my comment, I do not think this is a good idea. Yes, WordPress' loop comes to mind, but it just feels dirty to me.

What I would suggest are Callbacks / Callables. This way you do not need to start and end the loop in different functions and can simply pass a function to call inside the loop. I have prepared this working example. Your code could look like this

PSMQuery::loopQuery("Data", function($data) {
    // return something
});

//***

public static function loopQuery($data, callable $callback) {
   while (loop) {
       // do something with $callback
   }
}

If you want to manipulate in a more complex way, references might help you find a solution

Sign up to request clarification or add additional context in comments.

6 Comments

After being confused for a while I finally got it, thanks for the answer :)
@kero Still I would opt and drag about @JackHales soslution. Take for example wordpress. It would be much cleaner to have php structure like start_loop(); echo $title; end_loop().
@PawełWitek Even with WP you'd usually do while (have_posts()) { the_post(); /* your code */ }. In the end it is more a question of style of code I'd say - and I stick with my original message, that I prefer it with a callback, this way it is all combined and not one method call here, another one somewhere else.
@kero yes, you're right, I do that by myself, I've created snippet to print this out for ease of use and its easy however my thinking is purly esthetics :) and I wonder if this open / close loop functions are even possible? have a great day! ^^
@PawełWitek In templating you usually do it like this, that your calls get transpiled to while(): and endwhile;. With this, you can restructure it around multiple files/calls/whatever, but for me it always feels just too messy
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.