demuro1
demuro1

Reputation: 299

access parent function in extended class

I'm trying to become more competent with php. I have a question about parent classes. In the code below is there a way to call function foobar() from class a within the $b instantiation. Thanks

<?php 
  class foo {
    function callFooBar(){$this->foobar();} 
    function foobar(){echo('foobar of foo');}
  }
  class bar extends foo {
    function foobar(){echo('foobar of bar');}
  } 
  $b=new bar; 
  $b->foobar(); 
  echo("<br>");
  $b->callFooBar(); 
?> 

Upvotes: 0

Views: 61

Answers (1)

jeroen
jeroen

Reputation: 91734

If you want to call the method from the class itself, you can use self:

function callFooBar(){self::foobar();}

An example.

Using $this or static will give you the same results twice.

If you want to do it from a method in class bar, you have to use parent

Upvotes: 2

Related Questions