douliu1092 2018-04-16 19:34
浏览 9
已采纳

从更复杂的PHP函数中检索值

If I have code like this

 $myFunction = new classFoo();                             //ie. class
 $myFunction-> new functionBar($value1,$value2,$value3);   //ie. function

I understand in a normal scenario, if I had this function...

function functionBar($value1,$value2,$value3){
    $answer=$value1+$value2+$value3
    return $answer
}

I could then plug into it like this

$answer=functionBar(1,2,3)

I could expect an echo of $answer to be 6, correct?

If I'm wrong there let me know, because my real issue is that the function is wrapped in a variable, which if I examine through console has what I need in it, but I can't get my head around how to access it with that extra layer involved.

  • 写回答

1条回答 默认 最新

  • douke6881 2018-04-16 19:51
    关注

    because my real issue is that the function is wrapped in a variable [snip] but I can't get my head around how to access it with that extra layer involved.

    Using your code and var names, and I've added the function which you do understand inside the class (is simply a function (aka "method") which essentially does the same thing - there are differences but not within the scope of your question).

    It returns a value, which you can "wrap" (store) in a variable just the same as the function does without the class:

    $myFunction = new foo();
    $answer = $myFunction->functionBar(10, 20, 30);
    
    class foo
    {
        public function functionBar($value1, $value2, $value3)
        {
            return $value1 + $value2 + $value3;
        }
    }
    

    Your $answer var would contain "60". echoing it out would echo "60".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装