benanamen Posted January 10, 2017 Share Posted January 10, 2017 (edited) How would I to convert the following function to a class? What is the proper way to deal with GET and POST in a class since they are globals? My start class ShowActionMessage { } Function to convert function show_action_messages() { if (isset($_GET['insert'])) { $action = 'Added'; } if (isset($_GET['edit'])) { $action = 'Updated'; } if (isset($_GET['deleted'])) { $action = 'Deleted'; } if (isset($_GET['emailed'])) { $action = 'Emailed'; } if (isset($action)) { ?> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="success">Record <?= $action ?></div> </div> </div> <?php } } Edited January 10, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/302901-oop-convert-function-to-class/ Share on other sites More sharing options...
requinix Posted January 10, 2017 Share Posted January 10, 2017 Why do you think you need a class for this? And for what, exactly? Quote Link to comment https://forums.phpfreaks.com/topic/302901-oop-convert-function-to-class/#findComment-1541241 Share on other sites More sharing options...
benanamen Posted January 10, 2017 Author Share Posted January 10, 2017 This is just for learning OOP. It is not about the function itself really, but how to handle superglobals in a class. The issue actually came up in a login logging class that uses $_SERVER['REMOTE_ADDR'], another superglobal. Several code analyzers (scrutinizer, codacy, code climate ) pointed out a problem with using them in the class. * As far as what the function is for, it is used to display messages to the user after certain actions such as a db record added or deleted Quote Link to comment https://forums.phpfreaks.com/topic/302901-oop-convert-function-to-class/#findComment-1541243 Share on other sites More sharing options...
requinix Posted January 10, 2017 Share Posted January 10, 2017 If you have code that deals with superglobals (eg, a "Request" class) then use it. Otherwise it's not a crime to use them - just don't make changes. Quote Link to comment https://forums.phpfreaks.com/topic/302901-oop-convert-function-to-class/#findComment-1541244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.