php类操作符 -> 连续访问多个方法

作者:root 时间:24-05-13 阅读数:75人阅读
class a{
	 private $b = 0;
	  
	 public function c( $Num = 0 ) {
	   $this->b = $Num;
	   return $this; //关键就在这里,有这条就可以连续调用了,
	 }    
	 
	 public function d($d) {
	  	$this->b = $this->b+$d;
	  	return $this;
	 }   
 
	public function e(){
		echo $this->b;
	}
 }     
 
//接下来是调用
 $Obj = new a;
 $Obj -> c( 50) -> d(10)->e();		//调用运行后,页面会输出55
 
/*
在php中, return关键词通常会用于给函数方法返回值并终止该函数方法的运行
类对象中,$this表示当前对象, 那么return $this 等于就是给函数返回了类当前对象,而这个函数的最后结果也成了一个对象, 那对象,自然就能使用 -> 调用该对象中的成员了!
*/


本文链接:http://0595banjia.com/?id=34 

分享到: