-1

I am trying to set a variable's value within a class but doing this allows others do it as well, which I supposedly can't have (depending on if isolation is possible I'll give up or not). I landed on the idea of a filter but it turned out to be identical to a setter. The CMS is WordPress, but here's what a filter is:

In short, when you use add_filter to a value, anyone can hook their own functions to this filter specifically and there's an order. There's an original value and, based on how many functions were hooked to this filter, the value often turns out to be totally something else by the time all the functions have executed, so when does it execute? Exactly when you apply_filters - when this is called, all the functions that were hooked before it will change / play with that value and this is within the global state, meaning, everyone, everywhere has access to this.

It's clearly a trait of an event-driven system, every plugin / piece of code can change that value AFTER everything else has changed it themselves and pass it along to the next function.

Now, I have designed my class, within my system in this manner:

class ImportClass
{
    private $important_data;
}

without any setters at all. As such, that variable can only be changed by the class itself and no one else, no matter the inheritance. But, due to constraints of my system, I decided to put it in a filter, since my system needs to communicate to this class without initializing it with any parameters in its construct / setters, as such:

class ImportClass
{
    private $important_data;
    public function __construct() //empty, has to.
    {
        $this->important_data = apply_filters( 'important_data', '' );
    }
}

(Keep in mind that the value passed by apply_filters( 'important_data ') can be used in as many times as you like.)

But I don't want others to be able to mess with this value and I just gave them a way to do it. In fact, because of this approach, that private could be public. I want to isolate this part of the system at all costs because it's internals no one should mess with, under any circumstance.

I'm at crossroads. My system needs to communicate in a tamper-free manner to these classes without having to pass anything to them because if I implement ways for the value to be messed with, it renders the system useless.

Is there any way to achieve this or is it a trade-off I have to make to ensure my system works?

closed as unclear what you're asking by Martin Maat, Ben Cottrell, Bart van Ingen Schenau, BobDalgleish, gnat Jan 7 at 21:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What is the actual problem you're trying to avoid by preventing others from modifying it? How exactly would it render the system useless? i.e. what would break or go wrong if someone was able to modify it? – Ben Cottrell Jan 6 at 10:26
  • 2
    I don't get it. "My system needs to communicate to this class" makes you use applyfilters why? Let them call you and tell you things. But don't let that change your values unless you decide you want to. This is called 'tell don't ask'. Preserve your encapsulation. – candied_orange Jan 6 at 11:24
  • @candied_orange Ok, that's what I'm looking for. What's the code? That's my issue - I don't understand how that looks. – coolpasta Jan 6 at 11:25
  • After you build your object you pass a reference to it to things that want to talk to it. – candied_orange Jan 6 at 11:32
0

I see no need to do the filtering in your constructor. You could have a function to spit out the filtered data for other classes and this function does not have to modify the important data. Something like (pseudocode):

function GetFilteredData() {
    return apply_filters(important_data);
}

You can apply caching if the filters are costly to run, but rather try and keep it simple to start.

Another advantage of this approach is you avoid doing the filtering until someone actually asks for the filtered data.

I'm not sure if ImportClass should be taking care of the filtering of it's data. It could always have a method to expose a copy of it's data to another class which does the filtering. This is not the same as giving another class access to $important_data, because they cannot modify the data in the private variable. If the filtering code completely breaks, the original data is unaffected.

Not the answer you're looking for? Browse other questions tagged or ask your own question.