0
<?php

class Foo {
    private function FooFunction(){

      }
}

class Bar extends Foo {
    public function BarFunction(){
           $this->FooFunction();
       }

}

$foo = new Foo();
$foo->FooFunction();  //Fatal error: Call to private method Foo::FooFunction()
                      //(Fair enough)

$bar = new Bar();
$bar->BarFunction();  //Fatal error: Call to private method Foo::FooFunction()
                      //from context 'Bar' 

I'm having some difficulty understanding how to properly declare functions in a class which can then be used in an extension of that class

When I instantiate Foo I'd like FooFunction to remain private. However, I do need to be able to call it from within Bar.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
andrew
  • 9,313
  • 7
  • 30
  • 61

1 Answers1

2

change code as below:

<?php

class Foo {
    protected function FooFunction(){

      }
}

class Bar extends Foo {
    public function BarFunction(){
           $this->FooFunction();
       }

}

private methods not accessable in child class.
you need to use protected method type.

omid
  • 762
  • 8
  • 26