dskzap8756 2014-07-08 03:40
浏览 23
已采纳

使用相同的条目在同一个类中调用2个函数

lets imagine i have this functions

class xx {
    function one() {
        echo "hello";
    }
    function two() {
        echo "how are you";
    }
}

if i do this

$call = new xx;
$call->one();

this will print hello on the page but if i want to print hello how are you can i do something like this?

$call->one()->two();

??? because i tried that and it prints me hello only, how can i call the second function with one sentence only? also tryed

$call = new xx();
$calboth = $call->one();

$callboth->two();

but just print hellohello anyway, so how can i do it? to print both? my point is not printing but making 2 functions work in same call

for example function dbconnect and login or dbconnect and register, inside same class and include the class php on main file and call only with something like i said before

$login = new system;
$login->dbconnect()->login();

thanks for all awnsers

  • 写回答

1条回答 默认 最新

  • dongquxiao8545 2014-07-08 03:43
    关注

    To chain methods, you need to return an instance of the class:

    class xx {
        function one() {
            echo "hello";
            return $this;
        }
        function two() {
            echo "how are you";
            return $this;
        }
    }
    

    Then this should work:

    $call = new xx;
    $call->one()->two();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理