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

```
It allows you to add custom setters and getters in case you want to add extra functionality, and also is sometimes used to track the "dirty" state (if an object has been changed since it was loaded from the DB).

It´s also due to the fact that PHP does not have a "native" syntax for read only properties.

**Addition:**
I didn´t quite get my point across in my original answer that its in many cases due to how PHP works.

Consider this PHP example where we use metaprogramming to dynamically call custom setters.

```
class Foo {
    protected $bar;

    // use some metaprogramming magic to automatically call setter
    public function __set($key, $val){
        $setter = "set".ucfirst($key);
        if (method_exists($this, $setter)){
            $this->$setter($val);
        }
        else {
            throw new Exception("$key value cannot be set, looking for $setter in ".get_class($this));
        }
    }
    // enforce the Type of bar
    public function setBar(String $val){
        $this->bar = $val;
    }
}

$foo = new Foo();
// this should fail
$foo->bar = 12; 
// Catchable fatal error: Argument 1 passed to Foo::setBar() must be an instance of String, integer given

```

What I´m trying to get at here is that you are not a Bad Programmer for having public properties, but rather that most PHP frameworks dodge around the PHP object model that requires properties to be protected for inheritance and reflection to work in many cases.