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