PHP Private Function

Sheeraz Gul Feb 15, 2022
  1. Demonstrate the Use of private Function
  2. Demonstrate the Use of private Function in Inheritance
PHP Private Function

If some property or method is declared as a private visibility keyword in PHP, it can only be accessed within its class.

The Private function will only be accessed in the class in which it was defined and cannot be accessed out of class.

A major drawback of using the private function is that the child classes cannot inherit such functions; the private function should be used correctly.

The tutorial demonstrates the uses of the private function.

Demonstrate the Use of private Function

First, we will create a class with one private and one default public method. We will call the private function and then out of the class.

The Class:

<?php
class test_private
{
    // Public constructor
    public function __construct() { }

    // Declaring a private method
    private function TestPrivate() {
    echo "Hello! this is private function declared in test_private class"; 
    }

    // Default is always public
    function test()
    {
        $this->TestPrivate();
    }
}
?>

Calling the function TestPrivate() outside the class:

<?php
$testclass = new test_private;
$testclass->TestPrivate(); // This will cause fatal error.
?>

Output:

PHP Fatal error: Uncaught Error: Call to private method test_private::TestPrivate() from context '' in /home/znhT6B/prog.php:22 Stack trace: #0 {main} thrown in /home/znhT6B/prog.php on line 22 

Calling the function test() outside the class:

$testclass = new test_private;
$testclass->test(); // this will run the private function because it is called inside the class

Output:

Hello! this is private function declared in test_private class

As we can see, when the private function only works when it is called inside its class, in our code, we created a public function test to call the private function so we can call it outside the class.

Demonstrate the Use of private Function in Inheritance

It is impossible to call a private method declared into a child class. Private methods can only be called in the same class.

See example:

<?php
class test_private2 extends test_private
{
    // This is default public
    function test2()
    {
        $this->TestPrivate(); 
    }
}
$testclass2 = new test_private2;
$testclass2->test2(); // this will generate the same error.
?>

The Class test_private2 is the child class of the test_private class given in the code in the first example. We called the parent class a private function into child class.

Output:

Fatal error: Uncaught Error: Call to private method test_private::TestPrivate() from context 'test_private2' in C:\Apache24\htdocs\php private.php:25 Stack trace: #0 C:\Apache24\htdocs\php private.php(30): test_private2->test2() #1 {main} thrown in C:\Apache24\htdocs\php private.php on line 25

What if we override the private function by creating a new function with the same name in the child class? The answer to that is once a private function is declared, it cannot be overridden in child class.

<?php
class Cars 
{
    public function test() {
        $this->test_public();
        $this->test_private();
    }
    public function test_public() {
        echo "This is public function from Cars class.";
		echo "<br>";
    }   
    private function test_private() {
        echo "This is private function from Cars class.";
		echo "<br>";
    }
}

class Bikes extends Cars 
{
    public function test_public() {
        echo "This is public function from Bikes class.";
		echo "<br>";
    }   
    private function test_private() {
        echo "This is private function from Bikes class.";
		echo "<br>";
    }
}
$mybike = new Bikes();
$mybike->test();
?>

The code above tries to override both private and public functions in the child class, but only the public function will be overridden.

Output:

This is public function from Bikes class.
This is private function from Cars class.

As we can see from the output, the private function is not overridden from the child class.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Function