PHP Access Modifiers (original) (raw)

Summary: in this tutorial, you will learn about PHP access modifiers, including public and private, and understand the differences between them.

Introduction to PHP access modifiers #

In the objects and classes tutorial, you have learned about how to use the public access modifier with properties and methods.

PHP has three access modifiers: public, private, and protected. In this tutorial, you’ll focus on the public and private access modifiers.

The public access modifier #

When you place the public keyword in front of a property or a method, the property or method becomes public. It means that you can access the property and method from both inside and outside of the class.

The following example illustrates the public access modifier:

`<?php

class Customer { public $name;

public function getName()
{
    return $this->name;
}

}`Code language: PHP (php)

Try it

In this example, the Customer class has a public property ($name) and public method (getName()). And you can access the property and method from both inside and outside of the class. For example:

`<?php

class Customer { public $name;

public function getName()
{
    return $this->name;
}

}

$customer = new Customer(); $customer->name = 'Bob'; echo $customer->getName(); // Bob`Code language: PHP (php)

Try it

How it works.

The private access modifier #

To prevent access to properties and methods from outside of the class, you use the private access modifier.

The following example changes $name property of the Customer class from public to private:

`<?php

class Customer { private $name;

public function getName()
{
    return $this->name;
}

}`Code language: PHP (php)

Try it

If you attempt to access the $name property from the outside of the class, you’ll get an error. For example:

$customer = new Customer(); $customer->name = 'Bob';Code language: PHP (php)

Error:

Fatal error: Uncaught Error: Cannot access private property Customer::$nameCode language: PHP (php)

So how do you access a private property?

To manipulate the value of a private property, you need to define a public method and use the method to manage a private property.

Typically, you need to define two kinds of public methods to manage a private property:

By convention, the getter and setter have the following name:

getPropertyName setPropertyNameCode language: PHP (php)

The following Customer class defines the getter (getName) and setter(setName) to get and set the value of the $name property:

`<?php

class Customer { private $name;

public function setName($name)
{
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>&gt;</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this-&gt;name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>name;
}

public function getName()
{
    return $this->name;
}

}`Code language: PHP (php)

And the following shows how to use the setName() and getName() methods to set and get the value of the $name property:

`$customer = new Customer();

$customer->setName('Bob'); echo $customer->getName();`Code language: PHP (php)

Try it

Why do you need private property? #

It may be faster to use the public access modifier for properties instead of using a private property with the public getter/setter.

However, by using the private property, you can prevent direct access to the property from the outside of the class.

In addition, the getter/setter methods ensure that the only way to access the property is through these methods. And the getter/setter methods can provide custom logic to manipulate the property value.

For example, if you want the value of the $name property to be not blank, you can add the validation logic to the setName() method as follows:

`<?php

class Customer { private $name;

public function setName($name)
{
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>i</mi><mi>m</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">name = trim(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">im</span><span class="mopen">(</span></span></span></span>name);

    if ($name == '') {
        return false;
    }
 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>&gt;</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this-&gt;name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>name;

            return true;

}

public function getName()
{
    return $this->name;
}

}

$customer = new Customer();

$customer->setName(' Bob '); echo $customer->getName();`Code language: PHP (php)

Try it

In the setName() method:

Summary #

Did you find this tutorial useful?