Not the most interesting post but essential for OOP PHP development:
publicscope to make that variable/function available from anywhere, other classes and instances of the object.
privatescope when you want your variable/function to be visible in its own class only.
protectedscope when you want to make your variable/function visible in all classes that extend current class including the current class.
Our first class:
If we try accessing the properties, we get the following:
To show how the scope setting works with extended classes, we extend our Plant class with Flower and create a function to display our variables.
Using out function we get the following:
Changing our echo’d variable in the function display() to
$this->leaves we get nothing echo’d - as the variable is
private - only available to the class it is declared in.
public can be accessed and changed from outside the class. This cannot happen with
Taken from this stackoverflow question with a minor improvement.