0

This may seem pretty simple and I am sure it is, I am just not getting it.

I understand that protected properties in a class in PHP can only be accessed within the class itself and inherited classes. Here is my code so far:

class q {

    public $publicQ = "This is a public property";
    protected $protectedQ = "This is a proected property";

    public function displayProtected() {

        echo $this->protectedQ;

    }

}

$q = new q;
echo $q->publicQ; #prints "This is a public property"
echo $q->protectedQ; #nothing
$q->displayProtected();

I have read the documentation, looked at other answers on SO and the concept just is not clicking with me. What do protected properties actually do, why would we use them and why is my example not working?

Sethen
  • 11,140
  • 6
  • 34
  • 65
  • 1
    see: http://stackoverflow.com/questions/1020749/what-are-public-private-and-protected-in-object-oriented-programming – CraigTeegarden Mar 12 '13 at 20:37
  • The last line should actually display the protected string. Doesn't it? – Yogu Mar 12 '13 at 20:41
  • It does not, currently. – Sethen Mar 12 '13 at 20:41
  • public/protected/private is the contract of your class with the outside world: `public` you can call directly (= regular 'consumers' of your class), `protected` you need internally, but 'tinkerers` _may_ access them, `private` = don't touch this shit, it's imporant and (believed to be) beyond tinkerers abilities. – Wrikken Mar 12 '13 at 20:49

5 Answers5

3

Think of your public properties and methods as an API you expose to the outside world and private/protected ones as "inner workings" of your class that the outside world not only shouldn't be concerend with but shouldn't be able to mess with either.

Here comes the obligatory bad car analogy:

The methods you'd expose in a Car class could be driveForward() and driveBackwards(). Both of them would make use of a method called transmitTheDriveToTheWheels() but it shouldn't concern the car's users and shouldn't be accessed by them, so you'd "hide" it by making it private.

Your car would have an engine property. You definitely don't want someone to be able to replace the engine with a cute little kitty by going $car->engine = $kitty; so you'd make the engine private as well.

Finally, your car would have a mileage property. You want the user to be able to read the mileage but not to be able to modify it. So you make the mileage private and expose a public getMileage() method.

Now whether you want to use private or protected to encapsulate the "inner" stuff of your class, depends on whether you expect the class to be extended or not.

lafor
  • 12,472
  • 4
  • 32
  • 35
2

Protected fields can be inherited, but cannot be shown like echo $q->protectedQ; Private fields cannot be neither displayed nor inherited.

Iwo Kucharski
  • 3,735
  • 3
  • 50
  • 66
0

Protected functions make your class more flexible.

Think of a class that somewhere has to load some data. It has a default implementation, which reads the data from a file. If you want to use the same class, but want to change the way it gets its data, you can create a subclass and override the getData() funciton.

Yogu
  • 9,165
  • 5
  • 37
  • 58
0

The only really difference from public methods is, as you've discovered, that protected functions can only be accessed from within the class or another class in the inheritance tree.

You wan't to declare functions as protected when they are not meant to be used from outside the class. This is a language feature purely to make your code more understandable (easier to read) and less susceptible to bugs and misuse. There is nothing (in forms of functionality) that you can't accomplish using only public methods.

It is very useful if you're sharing your code with others or if it's some kind of library.

Specific to PHP there is a particular useful case when using PHP's magic getter and setter functions (http://www.php.net/manual/en/language.oop5.overloading.php#object.set).

public $a = '1';
protected $b = '2';

public function __get($name) {
    return $this->{$name}.' (protected)';
}

$obj->a; //1
$obj->b; //2 (protected)

As per example, you can "protect" you variables and catch calls with the magic function. It's useful if you've published a class with a variable, and you later decide to do some preprocessing internally in the class before returning the variable.

Thomas Jensen
  • 2,635
  • 25
  • 38
0

You use protected/private methods to contain functionality to make your code easier to read and prevent repeating the same functionality in your public methods.

Making properties protected protects the object from being modified from outside unless you provided access via a setter.

You get more control over how your object is able to be used.

Schleis
  • 41,516
  • 7
  • 68
  • 87