Question: Hello I was wondering why some popular PHP libraries make all properties protected, then add get and set methods for them like:

```
  protected
    $a = false;

  public function getA(){
    return $this->a;
  }

  public function setA($value){
    $this->a = (bool)$value;
  }

```

What's the benefit of this and why not simply make the property public?

Answer: 
**OOP real world scenario:**

Imagine you have a class `Vehicles` and they have (`protected`) `wheels`. You have different `Vehicles` with `wheels`, but to `addWheel` to a `Bike`, is different from `addWheel` to an `Aircraft`.

Code advantage:

Using getter and setter, you can use `typehinting`.

**Compare those snippets:**

```
class Car {
    public $wheels;
}

$bmw = new Car;
$bmw->wheels = 'piece of paper';

```

The above code let's you add anything as a `wheel`, but can you use a piece of paper as a wheel?

Now with getter and setter:

```
class Car {
    protected wheels;
    public function __construct() {
        $this->wheels = new ArrayIterator;
    }

    public function addWheel(Wheel $wheel) {
        $this->wheels->add($wheel);
        return $this;
    }
    public function removeWheel(Wheel $wheel) {
        $this->wheels->remove($wheel);
        return $this;
    }
 }

 class Wheel {
 }

 $bmw = new Car;
 $bmw->addWheel('piece of paper'); // <-- throws an error!
                                   // paper cannot be used as a wheel

 $bmw->addWheel(new Wheel); // <-- good :-)

```

More code, to be more straightforward. Imagine you have `RearWheels` and `FrontWheels`:

```
class Wheel {
}

class FrontWheel extends Wheel {
}

class RearWheel extends Wheel {
}

class Car {
    protected wheels;
    public function __construct() {
        $this->wheels = new ArrayIterator;
    }

    public function addWheel(Wheel $wheel) {
        // check for already existing Wheels here.
        // Pseudo Code:
        if (wheels typeof RearWheel > 2) {
            throw new Exception('cannot add more than 2 RearWheels to the Car');
        }
        return $this;
    }
    public function removeWheel(Wheel $wheel) {
        $this->wheels->remove($wheel);
        return $this;
    }
 }

```
Popular libraries which support their classes to be extended or inherited make their fields protected. This is concept of OOP that there must be abstraction and encapsulation in terms of data. Everyone shall not be allowed to directly access the data. Only member of a class shall access its data. This is reason fields are marked protected so that only inherited class's objects or same class's objects shall have access to data. All others must use get and set methods for reaching data. Keeping the code managed, clean and safe.