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();
?>
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