Home About Contact DOTW RSS

Get Social with GeekZenith...

OOP in PHP Part 3: Field Scopes

Object-Oriented Programming in PHP

I briefly touched upon field scopes (otherwise known as ‘access modifiers’) in my first OOP in PHP tutorial and this piece will expand on this small – yet entirely nifty – OOP functionality in an effort to explain what precisely field scopes are and what they do.

Field Scopes Explained

PHP supports five different types of field scopes: public, private, protected, final and static, but for this beginners tutorial we’ll only be looking at the first three (public, private and protected). Field scopes allow the user to determine the levels of access for a given field or property to be used within PHP classes.

For example, imagine you’ve created a PHP class and you intend to build upon in using the extend function but you don’t require all the variables to be shared amongst each extension due to potential naming conflicts or indeed you simply wish to restrict the field from being utilised outside of that particular class.

Field scopes allow you to set and control such restrictions.

Public

The Public field scope is the set default and as you might be able to deduce from its title, this declares the field within public scope and allows you to utilise this field within the class and all set extensions of the class.

Take the classes I created in my second OOP in PHP tutorial for example. In that tutorial I discussed the use of extensions and demonstrated the use of the Public function (see below):



class VAT {

	public $vat;

	function CalcVAT($figure) {
	$this->vat = ($figure * 1.175) - $figure;
	}

	function GetVat() {
	return $this->vat;
	}

}

class TotalVAT extends VAT {

	function TotVAT($figure)
	{
	$this->vat = $figure * 1.175;
	}
}

As you can see the $vat field was declared public in the VAT class and therefore was able to be reused without re-declaring the field in TotalVAT extension.

Private

The private field scope only allows the user to reuse the field within that class only. Therefore, if we were to change the above field to private instead of public, only the field could only be used within the VAT class and not the TotalVAT class.

The syntax for the private is much the same as the Public field declaration:


private $variable

Protected

The Protected field scope is a lot similar to the Public field scope, in so much as the same field can be defined in a class and still be used in all extensions of the class. However, the difference between the Public and the Protected field scope is that the public can be directly accessed via the class, whereas the Protected field can only be viewed once it’s been auctioned by a method. Basically, the public field scope has visibility, usability and modifiable and the protected field scope is usable and modifiable.

The best example of these differences can be found in the php.net manual or see this example below:


/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

As you can see from this example, the fields are all declared: public, private and protected. All three fields are visible via the printHello method, but when each field is called to directly, only the Public field returns a result (the others actually cause a fatal error and prevent anything further being called to).

If you have any further questions, requests or pleas for help, then please comment below or get in touch via the GeekZenith.com contact page.

Like this fantastical post on Facebook!

Share this awesometastic post with your friends on Facebook!

4 Responses to “OOP in PHP Part 3: Field Scopes”

  1. Your Name... says:

    Ummm, you seem to be missing some code in your last example.

  2. The Master of Awesomeness says:

    Good spot, sorted.

  3. gotnospirit says:

    protected is much similar to private than to public.
    w/ protected you can also directly access the member in subclasses (which is not allowed w/ private).
    You also said that public “allows you to utilise this field within the class and all set extensions of the class” which is true but it mainly permits direct access from the “outside world” (shown by the php.net example).
    Last point, you should always use a modifier in PHP 5 even if the member’s visibility is public :)

    btw, it’s a cool beginners’ tutorial

  4. The Master of Awesomeness says:

    Hey there!

    Thanks for your input, much appreciated :-)

Leave a Reply