Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • OOPS Visibility concept of properties in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 84
    Comment on it

    In this tutorial we will see the visibility of the properties which are defined as public , protected or private. The property of a class that is defined as public can be accessed from any where restricting the visibility of property which are defined as protected limited to the child class only and if the property is defined as private it can be visible only within the same class. Below are is the example that depicts the above mentioned scenarios.Let us create a file test.php


    Example:

    class A {
    
    public $public = "This is public variable of class A";
    protected $protected = "This is protected variable of class A";
    private $private = "This is private variable of class A";
    
    
    function testFunction()
        {
            echo $this->public;echo '  inside testFunction of A <br>';
            echo $this->protected;echo '  inside testFunction of A <br>';
            echo $this->private;echo '  inside testFunction of A <br>';
        }
    
    }
    
    $objA = new A();
    
    echo $objA->testFunction();
    echo $objA->public;echo '<br>';
    //echo $objA->protected;echo '<br>'; //Fatal error: Cannot access protected property A::$protected in the ../test.php line no.
    //echo $objA->private;echo '<br>'; // Fatal error: Cannot access private property A::$private in the ../test.php line no.
    echo "<br>";echo "<br>";
    
    OUTPUT: 
    
    This is public variable of class A inside testFunction of A
    This is protected variable of class A inside testFunction of A
    This is private variable of class A inside testFunction of A
    This is public variable of class A
    

    Extending the class B

    class B extends A{
    
    public $public1 = "This is a public variable of class B";
    protected $protected1 = "This is a protected variable of class B";
    private $private1 = "This is a private variable of class B";
    
    
    function testFunction()
        {
            echo $this->public;echo '  inside testFunction of B <br>';
            echo $this->protected;echo '  inside testFunction of B <br>';
            echo $this->private1;echo '  inside testFunction of B <br>'; 
        }
    
    }
    
    $objB = new B();
    
    echo $objB->testFunction();
    echo $objB->public;echo '<br>'; // accessed public variable of Class A 
    echo $objB->public1;echo '<br>'; // accessed public variable of Class B
    //echo $objB->protected1;echo '<br>'; //Fatal error: Cannot access protected property B::$protected1 in the ../test.php line no.
    //echo $objB->private1;echo '<br>'; //Fatal error: Cannot access private property B::$private1 in the ../test.php line no.
    echo "<br>";echo "<br>";    
    
    OUTPUT: 
    
    This is public variable of class A inside testFunction of B
    This is protected variable of class A inside testFunction of B
    This is a private variable of class B inside testFunction of B
    This is public variable of class A
    This is a public variable of class B
    

    Class C extending Class B

    class C extends B{
    
    
    public $public2 = "This is a public variable of class C";
    protected $protected2 = "This is a protected variable of class C";
    private $private2 = "This is a private variable of class C";
    
    
    function testFunction()
        {
            echo $this->public;echo '  inside testFunction of C <br>';
            echo $this->public1;echo '  inside testFunction of C <br>';
            echo $this->public2;echo '  inside testFunction of C <br>';
            echo $this->protected;echo '  inside testFunction of C <br>';
            echo $this->protected1;echo '  inside testFunction of C <br>';
            echo $this->protected2;echo '  inside testFunction of C <br>';
            echo $this->private2;echo '  inside testFunction of C <br>';
            //echo $this->private1;echo '  inside testFunction of C <br>'; //Notice: Undefined property: C::$private1 in the ../test.php line no.
        }
    
    }
    
    $objC = new C();
    echo $objC->testFunction();
    echo $objC->public;echo '<br>';
    echo $objC->public1;echo '<br>';
    echo $objC->public2;echo '<br>';
    
    echo "<br>";echo "<br>";
    
    OUTPUT:
    
    This is public variable of class A inside testFunction of C
    This is a public variable of class B inside testFunction of C
    This is a public variable of class C inside testFunction of C
    This is protected variable of class A inside testFunction of C
    This is a protected variable of class B inside testFunction of C
    This is a protected variable of class C inside testFunction of C
    This is a private variable of class C inside testFunction of C
    This is public variable of class A
    This is a public variable of class B
    This is a public variable of class C
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: