How can a PHP class that extends another inherit a private function?

class, inheritance, php

Solution

`bar` is inheriting the function alright, but it can't call it. That's the whole point of `private` methods, only the declaring class can call them.

Problem

I am trying to extend a PHP class without rewriting the whole thing. Here is an example: ``` <? $a = new foo(); print $a->xxx(); $b = new bar(); print $b->xxx(); class foo { const something = 10; public function xxx() { $this->setSomething(); return $this->something; } private function setSomething() { $this->something = self::something; } } class bar extends foo { public function xxx() { $this->setSomething(); $this->something++; return $this->something; } } ?> ``` However when I run the script I get the following error: ``` Fatal error: Call to private method foo::setSomething() from context 'bar' in test.php on line 23 ``` It would seem that bar is not inheriting the private function setSomething(). How would I fix this without modifying the foo class?

Original source

Related problems