<?php
    class Test
    {
        private $a = 10;
        public $b ='abc';
}
class Test2 extends Test
{
    function __construct()
    {
         echo $this->a;
         echo $this->a = 20; // wh
    }
}
$test3 = new Test2();
            Asked
            
        
        
            Active
            
        
            Viewed 43 times
        
    -7
            
            
         
    
    
        tereško
        
- 58,060
- 25
- 98
- 150
2 Answers
3
            
            
        echo $this->a;
echoes value of class property a. This property is not defined, because property a of class Test is private and therefore is not available in class Test2. So, property a is created in class Test2.
echo $this->a = 20; // wh
does the next: assigns 20 to a property (which was created on the previous line) and echoes result of assignment which is 20.
The solution:
class Test
{
        // protected property is avalilable in child classes
        protected $a = 10;
        public $b ='abc';
}
class Test2 extends Test
{
    function __construct()
    {
         echo $this->a;
         $this->a = 20;
         echo $this->a;
    }
}
$test3 = new Test2();  // outputs 10 20
 
    
    
        u_mulder
        
- 54,101
- 5
- 48
- 64
- 
                    1Bleh. I close voted for the wrong reason. I should have duped this on https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected – Machavity Oct 14 '17 at 13:46
- 
                    thanks for your help – Abhishek Joshi Oct 14 '17 at 14:53
-1
            
            
        You should change
private $a = 10;
to:
protected $a = 10;
 
    
    
        Walid Ajaj
        
- 518
- 4
- 8
- 
                    Sure, but the question is "why"? You could learn a thing or two from [u_mulder's answer](https://stackoverflow.com/a/46745074/1415724). – Funk Forty Niner Oct 14 '17 at 13:43
