1

Can't explain why this prints "\n1". Private property can't be inherited. That's why constructor should set value of new defined public proverty, but "print $b->number" prints nothing whereas "$b->printNumber()" prints "1".

class A { private $number; public function __construct($number) { $this->number = $number; } public function printNumber() { print $this->number; } } class B extends A { public $number; } $b = new B(1); print $b->number; print "\n"; $b->printNumber(); 
0

1 Answer 1

3

The private is defined in "A". As printNumber() is also defined in "A" it will access $this->number in the scope of class "A". So the result is expected.

class B extends A { public $number; public function __construct($number) { $this->number = $number; } } 

Of course you would never override a private with a public ;)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.