-1

So I have this hierarchy :-

class base{

    function something(){
    }

    function something2(){
    }
}

class api extends base{

   function send(){
   //how do I call the function "send" from "message" class 
     within this current function (Send function of api class)
   }
}


class message extends api{
    function send(){
    //do something
    }
}

how do I call the function send() of class message from within the function send() of class api ?

NikloYa
  • 49
  • 4
  • 8
  • 1
    Possible duplicate of [PHP: How to call function of a child class from parent class](https://stackoverflow.com/questions/1944827/php-how-to-call-function-of-a-child-class-from-parent-class) – Yupik Sep 29 '17 at 07:49
  • 1
    Possible duplicate of [What does the variable $this mean in PHP?](https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php) – Ronnie Oosting Sep 29 '17 at 07:49
  • Possible duplicate of [multiple ways of calling parent method in php](https://stackoverflow.com/questions/11237511/multiple-ways-of-calling-parent-method-in-php) – rndus2r Sep 29 '17 at 07:49
  • Check [PHP: How to call function of a child class from parent class](https://stackoverflow.com/questions/1944827/php-how-to-call-function-of-a-child-class-from-parent-class) – Nana Partykar Sep 29 '17 at 07:57

2 Answers2

1

If you want to call the parent instance of send from within the message call the following can be done.

<?php

class message extends api{
    function send(){
        return parent::send() //This will call the send() method in api
    }
}

However if you are just inheriting the same functionality the above is not required, so the following can be done.

<?php

class message extends api{
    //notice no send method
}

$message = new message();
$message->send(); //Still calling the send() method in api

I would strongly recommend however follpwing naming conventions and formatting your class names to StudlyCaps. More information on that available here: http://www.php-fig.org/psr/psr-1/


On reread it seems you are looking for class abstraction. Basically a way of a parent to 'know' what its child classes implement. The following architecture can be defined.

<?php

//Notice 'abstract' added before the class
abstract class api extends base{

   /**
    * Defining this abstract method basically ensures/enforces
    * that all extending classes must implement it.
    * It is defined in the 'parent', therefore the parent knows it exists.
    */
   abstract protected function doSend();

   function send(){
       //This calls the 'child' method
       //This could be defined within the message class or any other extending class
       return $this->doSend();
   }
}

/**
 * Becuase we extend the new abstract class, we must implement its abstract method
 */
class message extends api{
    protected function doSend(){
        //Do something here
    }
}

Then the following can be done.

<?php
$message = new message();
$message->send(); //Calls the api send() method, which then calls the message doSend() method.
JParkinson1991
  • 1,256
  • 1
  • 7
  • 17
0

So you can send complete object of message to api class and then you can get your send method from Message Class in the Api class function.

Like

$message_object = new message();
$api_object = new api();
$api_object->send($message_object);

Now Your api send message will be like below

class api extends base{
   function send(message $message){
     // You can access "send" function of "messag"e class like in below.
     $send = $message->send(); 
   }
}

Hope it will help.