Question from the Object Oriented Programming test

How to call a method from a parent class in a child class in PHP

Expert

What will the following pseudo-code display?

class A 
{
    public function foobar() {
       echo this->foo();
       echo this->bar();
    }

    private function foo() {
       return "A::foo\\n";
    }

    public function bar() {
       echo "A::bar\\n";
    }
}

class B extends A 
{
    private function foo() {
       return "B::foo\\n";
    }

    public function bar() {
       echo "B::bar\\n";
    }
}

var toto = new B();
toto->foobar();
Author: Eric HostaleryStatus: PublishedQuestion passed 1343 times
Edit
0
Community Evaluations
developer avatar
Najmul
01/10/2023
The provided pseudo-code contains some syntax errors, such as the use of 'this' instead of '$this' to refer to object properties and methods. In this code, class B overrides the foo() method while extending class A, and it also overrides the bar() method. When you create an instance of class B and call its foobar() method, it calls the overridden foo() and bar() methods from class B. I think the answer is: B::foo B::bar
developer avatar
Auteur anonyme
08/04/2022
it's in french when i'm using the site in english
developer avatar
Auteur anonyme
12/04/2022
Hi, sorry for this french. This quiz is not yet translated. We will make it easier to find english test soon.