BB-8
BB-8

Reputation: 625

Call a function from another function in PHP

I am struggling to implement a php code with the following structure:

public function hookActionValidateOrder($params)
{
     $invoice = new Address((int)$order->id_address_invoice);
     $myStreet = $invoice->address1;
     $myCity = $invoice->city;
     $myPostcode = $invoice->postcode;

     // ... SOME IRRELEVANT CODE HERE ...

     $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
}

public function send($a, $b, $c)    /* function send($a, $b, $c) */
{
     // ... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
}

The problem is, this code doesn´t seem to work. When I put it into a code validator, it says: "Function 'send()' does not exists". Tell me please Why is that so and how do I fix that?

Upvotes: 6

Views: 49401

Answers (3)

gaurav
gaurav

Reputation: 1339

If you are using a class, then you can use $this for calling the function:

class Test {

    public function say($a) {
        return $a ;

    } 

    public function tell() {
        $c = "Hello World" ;
        $a = $this->say($c) ;
        return $a ;
    }
} 

$b= new Test() ;    
echo $b->tell() ;

If you are using a normal function, then use closure:

function tell(){
   $a = "Hello" ;
   return function($b) use ($a){
      return $a." ".$b ;
   } ;  
}

$s = tell() ;
echo $s("World") ; 

Upvotes: 14

user7608568
user7608568

Reputation:

if you trying to use a Function or any code from the original page
in a secondary page without including it , then usually it show you this Error

(.... ' string name , function Name ,.......etc ') does not exists


The solution is to include it first in your secondary page and use :

include("yourpage.php");

if not then
see "@Gulshan" Comment about "extends" classes. it causes the same problem

Upvotes: 1

Gulshan S
Gulshan S

Reputation: 1094

try this:

class test 
{
public function hookActionValidateOrder($params)
    {
         $invoice = new Address((int)$order->id_address_invoice);
         $myStreet = $invoice->address1;
         $myCity = $invoice->city;
         $myPostcode = $invoice->postcode;

         // ... SOME IRRELEVANT CODE HERE ...

         $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
    }
  }
class test1 extends test
{
    public function send($a, $b, $c)    /* function send($a, $b, $c) */
    {
         // ... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
    }
}

You can solve this with extends one class to another class .

Upvotes: 0

Related Questions