简体   繁体   English

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

[英]call 2 functions inside same class with the same entry

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? 但是无论如何只要打印hellohello,我该怎么做? 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 例如函数dbconnect和login或dbconnect和register,在同一个类中,并且在主文件中包括php类,并且仅使用类似我之前所说的内容进行调用

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

thanks for all awnsers 感谢所有遮阳篷

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel Botman 问题 - 在 botman 类中嵌套后无法调用来自同一类的函数 - Laravel Botman issue - Can't call functions from the same class after nesting inside botman class 如何从同一个 class 中的 function 中调用 class 中的 function? - How to call a function inside a class from within a function in the same class? 正则表达式以匹配相同或父类函数的函数调用 - Regex to match function call of same or parent class functions 如何在PHP中该类的另一个函数内调用同一类的函数 - how to call the function of the same class inside another function of that class in php php oop 从同一方法内部调用方法 class - php oop call method from inside the method from the same class 调用同一个类的两个方法 - Call two methods of the same class Oneliner在PHP中使用相同的参数调用多个函数 - Oneliner to call multiple functions with the same argument in PHP php调用类方法来自同一个类中的静态方法,但是没有实例化 - php call class method from static method inside same class but non instantiated 我正在尝试在Codeigniter中使用同一类中的两个函数将一个函数内的变量访问到另一个函数 - I am trying to access variables inside one function into another function with both the functions in the same class in codeigniter 如何为同一类中的所有函数运行代码? - How to run a code for all functions in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM