Question du test Programmation Orientée Objet

Comment appeler une méthode d'une classe parente dans une classe enfant en PHP

Expert

Qu'est ce que le pseudo-code suivant va afficher ?

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();
Auteur: Eric HostaleryStatut : PubliéeQuestion passée 1255 fois
Modifier
0
Évaluations de la communauté
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.