RuleBritannia Posted December 20, 2013 Share Posted December 20, 2013 Hello If I have 5 methods, But 4 of them can only run once one has been run and returned true, Whats the best OOP approach for this. I have this working solution, But I dont know if its what the professionals would do. class oop { var $status; function main() { $this->status = true; } function subone() { if($this->status =='true') { other stuff here etc } } function subtwo() { if($this->status =='true') { other stuff here etc } } } I have checked alot of oop sources, but i don't see a set out way to tackle conditional oop methods Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/ Share on other sites More sharing options...
gristoi Posted December 20, 2013 Share Posted December 20, 2013 class oop { private $_status; public function __construct() { $this->_status = false; } public function main() { $this->_status = true; } public function subone() { if($this->status) { // do stuff } } public function subtwo() { if($this->status) { // do stuff } } } Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462821 Share on other sites More sharing options...
trq Posted December 20, 2013 Share Posted December 20, 2013 You could implement a filter chain of sorts. I have a very simple example implementation in my old (dead) framework, Proem: https://github.com/proem/proem/tree/develop/lib/Proem/Filter And some docs: http://proemframework.org/docs/current/filter.html Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462824 Share on other sites More sharing options...
ignace Posted December 20, 2013 Share Posted December 20, 2013 I don't understand why everyone seeking help always ends up giving canned examples. A canned example is a canned answer. Tells us what you are doing, and why you want to do it this way. Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462830 Share on other sites More sharing options...
RuleBritannia Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) @trq, Ok reading this now, Seems quite in depth from my first glance at some github files, Only started OOP about 2 days ago. @ignace, I give a "canned" example to allow for a more precise comprehension for the reader, hoping that if they are more competent they will see it and say "no, what your trying to achieve is not best done like this, Its better approached like blabla...." even though the "canned" example approach may work. If the "canned" example I gave turns out to follow a good/best protocol, then I guess it cant be improved, But I have only started OOP 2 days ago, And in reading around 10 OOP tutorials, This wasn't covered. Before I go implementing bad techniques, I would rather learn the best way from the outset. Edited December 20, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462831 Share on other sites More sharing options...
trq Posted December 20, 2013 Share Posted December 20, 2013 @trq, Ok reading this now, Seems quite in depth from my first glance at some github files, Only started OOP about 2 days ago. Its implemented in just a couple of classes if you disregard the interfaces. Don't try ripping it out and using it, try to understand what it is an how it works. The idea is quite simple. Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462834 Share on other sites More sharing options...
RuleBritannia Posted December 20, 2013 Author Share Posted December 20, 2013 Its implemented in just a couple of classes if you disregard the interfaces. Don't try ripping it out and using it, try to understand what it is an how it works. The idea is quite simple. Ok noted, Also I never rip out work for what you mentioned. (Altough that probably explains why it takes me months to get anything "done") Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462835 Share on other sites More sharing options...
trq Posted December 20, 2013 Share Posted December 20, 2013 The only reason I said not to simply rip it out is because it wont' work stand alone. That particular implementation it pretty tightly coupled to the Service layer (which it shouldn't be, but thats another story), so you would need all of that as well which you don't want. Besides, the pattern itself is VERY simple and could be implemented in just a few lines of code really so.... Quote Link to comment https://forums.phpfreaks.com/topic/284877-best-way-to-tackle-conditional-methods-oop/#findComment-1462838 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.