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".

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

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃