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?

4
  • 1
    see: stackoverflow.com/questions/1020749/… Commented Mar 12, 2013 at 20:37
  • The last line should actually display the protected string. Doesn't it? Commented Mar 12, 2013 at 20:41
  • It does not, currently. Commented Mar 12, 2013 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. Commented Mar 12, 2013 at 20:49

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This made it make sense to me.
2

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

2 Comments

Then how would I show them, or are they not meant to be shown??
Using a special function in class, like getter.
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.

Comments

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.

Comments

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.

2 Comments

So, what purpose do protected functions even serve, then?
As I said, readability, easier maintenance and less error prone code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.