Question

Completely new to OOP programming, so sorry if the answer is obvious. I've seen many YouTube tutorials where the code is as following:

class myClass{
    private $myVar;

    function getVar(){
        return $this->myVar;
    }
}

myObject = new myClass();
print myObject->getVar();

but why is it preferable to make the variable private and then access it through a function, rather than just using myObject->myVar?

Was it helpful?

Solution

A few benefits of private variables:

  1. Nobody can change it from the outside without your permission. If this variable is important for internal state, having somebody externally access it can be disastrous when you need to use it next (because of a different method call on your class). This is a big deal.
  2. If somebody calls "get" on it, you can intercept it and create side effects if needed (for accounting reasons for example; note that "magic" behind the scenes is generally not a good idea from just a pure accessor).
  3. The value returned from a "get" may not be a real concrete value, but the result of a calculation. You expose it via the method, but it doesn't really need to be cached internally.

You may also see here.

Was this helpful?

OTHER TIPS

In the first case you have a getter, not a setter, so you could "read only" the private $myVar. In the second case $myVar is public so you can just set and get myVar.

Maybe read more about getter and setter (even on stackoverflow) to understand, why using getter and setter in OOP Design Pattern is more effective :)

A get and set function might do more than just get and set. For instance, how about something like this:

class myClass {
    private $myVar;
    public function setVar($val) {
        if( !is_numeric($val)) throw new Exception("Input must be a number");
        $this->myVar = 0+$val;
    }
}

If $myVar were public, I could set any value I wanted, not just numbers, which could cause errors later. This is just one possible application, there are uncountably many uses ;)

The larger your projects become, the greater the number of interactions that can occur between different objects. You want to control and limit these interactions to the bare minimum else difficult to discover side effects can and will creep into your code.

For example, you can have a class to calculate taxes of given amount of money. You wouldn't want anyone to accidently change 0.05 tax amount to something like 3.00 and bill the customer triple amount of item's price, or change the type of tax variable to something like NULL and cause exceptions/fatal errors during calculation.

With a settler, you can force people to obey your rules. You can validate everything before assigning values to private variables.

Usually, a good practice approach is: make everything private - then make public what needs to be public - and add required gettlers and settlers for necessary privates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top