欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解PHP神奇又有用的Trait

程序员文章站 2023-12-16 17:32:34
php和java,c++一样都是单继承模式。但是像python,是支持多继承(即mixin模式)。那么如何在php中实现多继承模式?这就需要使用trait。 tr...

php和java,c++一样都是单继承模式。但是像python,是支持多继承(即mixin模式)。那么如何在php中实现多继承模式?这就需要使用trait。

trait arrayabletrait{
	public function toarray(){

	}
}

class model{
	use arrayabletrait;
}


$model = new model();
$model->toarray();

trait使用场景

  1. 有些功能不需要类的方法属性,但是在不同的类都有使用需求。例如上面的对象转数组方法。这种情况可以使用一个基类定义toarray方法,则需要将这类基础方法定义在尽可能顶层的基类当中,保证所有的类都能够调用这个方法。
  2. 类因为某些需求,已经继承了第三方类对象。例如第三方orm模型类。这种情况如果要给类附加一些公共的功能,除了创建一个继承于orm模型的基类,复制一套公共功能的代码之外,就可以使用trait。

trait使用注意

方法优先级

trait arrayabletrait{

	public function logname(){
		return 'trait:'.$this->name;
	}

	public static function staticlog(){
		return 'trait:'.self::$staticname;
	}
}
class obj{
	protected $name = 'obj';
	public static $staticname = 'obj';
	public function logname(){
		return 'obj:'.$this->name;
	}
}
class model extends obj{
	protected $name = 'model';
	public static $staticname = 'model';
	use arrayabletrait;

	public function logname(){
		return 'model:'.$this->name;
	}
	public static function staticlog(){
		return 'model:'.self::$staticname;
	}
}

class model2 extends obj{
	protected $name = 'model2';
	public static $staticname = 'model2';
	use arrayabletrait;
}

$model = new model();
$model2 = new model2();
echo $model->logname()."\n";
echo $model2->logname()."\n";
echo model::staticlog()."\n";
echo model2::staticlog()."\n";

上面输出内容分别为model:model,trait:model2,model:model,trait:model2.可以看出,trait方法优先级为 当前对象>trait>父类,以上规则同样使用于静态调用。

属性定义要特别小心!!trait中可以定义属性。但是不能和usetrait当前类定义的属性相同,否则会报错:define the same property。但是,如果父类使用了trait,子类定义trait中存在的属性,则没有问题。

trait arrayabletrait{
	public $logger='file';
	public function log(){
		return 'trait:'.$this->logger.$this->name;
	}
}
class obj{
	use arrayabletrait;
	protected $name = 'obj';

}
class model extends obj{
	protected $logger = 'redis';
}
$model = new model();
echo $model->log()."\n";

私有属性私有方法。triat中可以方位use类的私有属性私有方法!!

从以上可以看出,trait本身是对类的一个扩展,在trait中使用$this ,self,static,parent都与当前类一样,zend底层将trait代码嵌入到类当中,相当于底层帮我们实现了代码复制功能。

多个trait相同方法。

trait arrayabletrait1{
	public function log(){
		return 'trait1:'.$this->logger.$this->name;
	}
	public function logname(){
		return 'trait1:'.$this->name;
	}
}
trait arrayabletrait2{
	public function log(){
		return 'trait2:'.$this->logger.$this->name;
	}
	public function logname(){
		return 'trait1:'.$this->name;
	}
}

class model{
	public $name = 'model';
	use arrayabletrait1,arrayabletrait2{
		arrayabletrait1::log insteadof arrayabletrait2;
		arrayabletrait2::logname insteadof arrayabletrait1;
		arrayabletrait2::logname as logname1;
	}
	protected $logger = 'redis';
}
$model = new model();
echo $model->log()."\n";
echo $model->logname1()."\n";

多trait相同的方法,需要使用instanceof 指定使用哪个trait的方法。instanceof后面的使用的trait。可以使用as设置添加方法别名(添加,原有方法还是能调用!!)。as还可以改变方法的访问控制

arrayabletrait2::logname as private改为私有方法。

以上所述是小编给大家介绍的php神奇又有用的trait详解整合,希望对大家有所帮助

上一篇:

下一篇: