Jump to content

Recommended Posts

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.

 


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
}
}

}

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

@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 by RuleBritannia

@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.

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")

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....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.