代码语言:javascript复制
as可以修改traits里面方法的可见性
代码语言:javascript复制<?php
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
public function ok(){
echo "<p>tA:ok...</p>";
}
}
trait tB {
public function cc(){
echo "<p>tB:cc...</p>";
}
public function dd(){
echo "<p>tB:dd...</p>";
}
public function ok(){
echo "<p>tB:ok...</p>";
}
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
use tA,tB
{
tA::OK insteadof tB;
tB::ok as okTb;
aa as private;
bb as private bbPrivate;
}
}
abstract class Dog {
}
class Huskies extends Dog {
use tA,tB {
tB::ok insteadof tA;
}
}
$hanMM=new Student('韩梅梅','女');
$hanMM->bbPrivate();
解析:
tA语法糖里面的ok方法替换掉tB里面的ok方法。 tB语法糖里面的ok方法改名为okTb。 aa方法把修饰符替换成私有性private。 把bb方法改名为bbPrivate,并把修饰符改成private.
在trait里面使用其他trait
代码语言:javascript复制<?php
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
public function ok(){
echo "<p>tA:ok...</p>";
}
}
trait tB {
public function cc(){
echo "<p>tB:cc...</p>";
}
public function dd(){
echo "<p>tB:dd...</p>";
}
public function ok(){
echo "<p>tB:ok...</p>";
}
}
trait tC
{
use tA,tB
{
tA::ok insteadof tB;
}
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
use tC;
}
abstract class Dog {
}
class Huskies extends Dog {
use tC;
}
$hanMM=new Student('韩梅梅','女');
$hanMM->cc();
解析:
tC里面有两个语法糖(有六个方法),先替换掉冲突的ok不然会报错,然后输出即可(算是tC有六个方法).
语法糖里面有抽象方法怎么做?(实现)
代码语言:javascript复制<?php
header('Content-Type:text/html;charset=utf-8');
//trait里面可以有抽象方法
trait tA {
public function aa(){
echo "<p>tA:aa...</p>";
}
public function bb(){
echo "<p>tA:bb...</p>";
}
abstract public function ok();
}
//抽象类
abstract class Humanity {
public $name;
public $sex;
public $iq=10;
const BIRTHPLACE='地球';
static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系
//构造函数
public function __construct($name,$sex){
self::$counter ;
$this->name=$name;
$this->sex=$sex;
}
protected function chew($food){
echo "<p>{$food}已经被咀嚼完成!</p>";
}
//抽象方法
abstract public function eat($food);
static public function hello(){
echo '<p>您好!我是来自'.self::BIRTHPLACE.'的人类</p>';
}
}
class Student extends Humanity {
const BIRTHPLACE='火星';
public $studentId;
public function test($subject){
echo "<p>{$this->name}正在考{$subject}!</p>";
}
public function eat($food){
$this->chew($food);
echo "<p>{$this->name}正在快速的吃{$food}!</p>";
}
use tA;
public function ok(){
}
}
abstract class Dog {
}
class Huskies extends Dog {
use tA;
public function ok(){
}
}
$hanMM=new Student('韩梅梅','女');
$hanMM->aa();
解析: 重写ok方法,不然会报错。