How to call a method not defined in the abstract class, but in the class that inherits it

Asked

Viewed 96 times

-2

Suppose there is a class and in its constructor it is necessary to call a function that is only defined in classes that inherit it. What is necessary for this to happen?

Sample code:

interface A {
    public function hello();
}

abstract class B implements A {
    public function __contruct() {
        if (method_exists($this, "hello"))
        {
            $this->hello();
        }
    }
}


class C extends B {
    public function __contruct() {
        parent::__construct();
    }
    
    public function hello() {
        echo "Hello world!";
    }
}

$B = new C();

This code returns nothing, but should return "Hello World!".

PS: I thought about abstract methods, but also I didn’t get the expected return.

  • you have created an implementation rule and not implemented in the class! gives an error that ai.

  • another problem hello() is a class C method does not give class B! has problem too, ie if you are doing something you do not understand.

  • also has typo.

2 answers

1


The name of the class construction method is missing a s that is construct, but, the correct thing is you implement the method hello within the class B because it is she who implements the interface, pay attention to that.

<?php

interface A 
{
    public function hello();
}

abstract class B implements A 
{
    public function __construct() 
    {
        if (method_exists($this, "hello"))
        {
            $this->hello();
        }
    }
    public function hello() 
    {
        echo "Hello world!";
    }
}


class C extends B 
{
    public function __construct() 
    {
        parent::__construct();
    }
}

$B = new C();

I’m not sure what you want to do, but it’s pretty strange, all this, if you determine that your class implements a interface this should be followed, is a rule and should be done so, now the reasons that led them to do this may be quite questionable.

  • I apologize, the function oi was meant to be hello.. I forgot to change while writing the question.

  • In your case if you moved this is because you have typo is construct with s... the rest will work: https://ideone.com/HiIw6E @Viníciuslara

  • 1

    Oh Jesus. Erase the question, but hey... Thanks for your time.

  • I edit the answer, what you did is not correct @Viníciuslara ... the implementation should be done in the class that is the implements

  • I understand. What would be the right solution to this? I need the parent class to be able to display hello world defined by the daughter class.. but I can’t do this communication by the controller of the child class

  • I implemented how it should be @Viníciuslara is in the answer... now if you have a problem you have to post your real problem ...

Show 1 more comment

1

This code should not return Hello World!.

You cannot call an unknown method, that is, you cannot call a method that does not exist in the class itself. Even if it were technically possible it makes no sense conceptually and would be a mistake of logic.

Also it is calling in a static method (yes, the constructor is a static method), an instance method, so it does not know what to call.

I don’t know if it’s what you want, but that’s what you can do (I took the interface because it has nothing to do with the problem, and if you have the question undefined):

abstract class B {
    public function do() {
        $this->hello();
    }
    abstract public function hello();
}


class C extends B {
    public function hello() {
        echo "Hello world!";
    }
}

$B = new C();
echo $B->do();

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.