6

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". Why is that so and how do I fix that?

2
  • are you using any framework? Commented Feb 23, 2017 at 4:40
  • 1
    are these two functions encapsulated in a class ? Commented Feb 23, 2017 at 4:42

4 Answers 4

14

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") ; 
Sign up to request clarification or add additional context in comments.

Comments

1

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 .

2 Comments

it is extends not extands
sorry my mistake..i just updated. thanks for your suggestion :)
1

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

Comments

0

Just to clarify something here related to the initial question. I think it's not a problem of calling a PHP function from another PHP function but more a class Issue here. If you basically do something like this without a class:

<?php
function func_1($a,$b) {
    $result = $a+$b;
    return $result;
} 
function func_2($a1,$b1) {
    //Call func_1;
    $value = func_1($a1,$b1);
    return $value ;
}
$a = 2; $b=3;

$result = func_2($a,$b);

echo "The addition of $a and $b is:".$result;
?>

You will receive something like this as output:

The addition of 2 and 3 is:5

But if you use classes like in your case, you can pretty easily get stuck inside of a class. So you need to comply to the "class" logic if you will. In gaurav's example, you can't just use the say Function outside of the class like this below because it's not accessible:

$c = say("blabla");

you have to initiate the class, then you can access its functions like guarav said:

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

gaurav already gave you the answer but pointing out the difference between functions and classes is still important I think.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.