OOP Scope – Public, Private and Protected

In this post we will be exploring OOP scope in PHP.  At it’s broadest interpretation scoping means determining what methods and variables can be exposed, how and in what context.

Public means accessible to everywhere and everyone that needs the method or variable and is great for non-sensitive rudimentary application data.

Private is the opposite of public, meaning private variables and methods can only exist within the ‘scope’ of the class and cannot be accessed outside it. This is great for protecting specific data that is only going to be used within the class. For example salt keys, generating passwords etc. Anything in other words that has no business being available to everyone outside the scope of the class.

Protected offers the best of both worlds and allows you to access the method or variable outside the class. the

Now, just to confuse things you could if you really wanted access a private variable or method from outside a class as well. You can do this by calling a public method to return the function or variable.

Let’s look at a practical example of returning a public variable:

class Requests
{
	protected $ticketID;
	private $salt_key = "!65h8876p3";
	public  $requestTitle;

	public function getRequestTitle($reqTitle)
	{
        $this->requestTitle = $reqTitle;
		return $this->requestTitle;
	} 
}

What’s happening?

We begin by declaring our class name, Requests. Within our Requests class we have declared three variables each with different scope. For example we have a salt key – this could be used to add an extra layer of security for ticket refs to prevent somebody gaining access to info they shouldn’t. The $salt_key is only ever going to be used within the Requests class – you certainly wouldn’t want it exposed to your entire application for obvious reasons! Hence making it private makes complete sense. If you tried to access that variable directly outside the scope of the class you will get an error.

The $ticketID is protected because you may need to use it in other parts of the application for specific purposes but in some instances a private scope would suffice, The great thing about protected scope is you can extend its use to other classes that inherit from this one. Finally we have a public variable for $requestTitle and as the name suggests it is visible to all parts of the application provided the class has been instantiated.

What we also have is a public method that gets the request name and returns it. Not much point in that you may ask – but with this get function you can receive the variable, validate it and either return it to the user or if invalid return an error. We also assign our public $requestTitle to the argument included in our getRequestTitle method.

Accessing Class Variables

$requests = new Requests;

$requestTitle = "Dedicated Hosting";
	
$response = $requests->getRequestTitle($requestTitle);

echo $response;

What’s happening?

Here we are instantiating our class and assigning an object to it – $requests. We then declare a variable, $requestTitle, and call our public method, getRequestTitle, and get it to return to our $response variable and echo it to the screen.

Now here is an exercise for you: try accessing our private variable the same way we accessed our public method – you should get an error. Now, – try and think of a way how you might be able to still access a private variable. A clue is to use a public method within your class and access it that way.

In summary

We have explored how to define a class and set scopes for methods and variables. I hope you can now demonstrate the importance of setting scope for variables and can see how this approach can be used in your own projects.