[英]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.