在 PHP 中从另一个函数调用一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42407168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:35:52  来源:igfitidea点击:

Call a function from another function in PHP

php

提问by BB-8

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

我正在努力实现具有以下结构的 php 代码:

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?

问题是,这段代码似乎不起作用。当我将它放入代码验证器时,它说:“函数'send()'不存在”。请告诉我为什么会这样,我该如何解决?

回答by gaurav

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

如果您正在使用一个类,那么您可以$this用于调用该函数:

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") ; 

回答by PHP Ninja

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 .

您可以通过将一个类扩展到另一个类来解决此问题。

回答by PHP Ninja

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

如果您尝试
二级页面中使用函数或原始页面中的任何代码而不包含它,则通常会显示此错误

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

(.. ' 字符串名称 , 函数名称 ,.......etc ') 不存在


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


解决方案是首先包含在您的辅助页面中并使用:

include("yourpage.php");

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

如果不是那么
看到有关“@Gulshan”评论“扩展”类。它会导致同样的问题