php强类型作用,php类、接口、抽象、trait、强类型使用

declare(strict_types = 1);

//declare(strict_types=1);//php7严格模式,只规定当前文件使用严格模式

//function test(string/int/Obj/array):void/int/?int//返回值和参数可指定类型

/*

function test(string $a,int $b,Obj $obj,array $arr,boolean $boo):?int{

if($boo === true){

return 1;

}else{

return null;

}

}

*/

class Province

{

private $pid;

private $name;

private $citses = [];

public function getInfo(): string

{

return "pid:" . $this->pid . ",name:" . $this->name;

}

public function __construct(int $pid , string $name)

{

$this->pid = $pid;

$this->name = $name;

}

public function setCity(array $citses)

{

$this->citses = $citses;

}

/**

* 返回城市数组

* @return array

* @author zlbao<276363733@QQ.com>

* @date : 2018/10/22/022

*/

public function getCity(): array

{

return $this->citses;

}

}

class City

{

private $cid;

private $name;

private $province;

public function getInfo(): string

{

return "cid: $this->cid ,name: $this->name";

}

public function __construct(int $cid , string $name)

{

// echo $cid;echo $name;

$this->cid = $cid;

$this->name = $name;

// echo $this->name;

}

/**

* 设置省份

* @param Province $province

* @author zlbao<276363733@QQ.com>

* @date : 2018/10/22/022

*/

public function setProvince(Province $province)

{

$this->province = $province;

}

public function getProvince(): array

{

return $this->province;

}

}

//设置数据

$c1 = new City(1 , "呼和浩特");

$c2 = new City(2 , "包头");

$c3 = new City(3 , "巴盟");

$p = new Province(1 , "内蒙古");

//设置关系

$c1->setProvince($p);

$c2->setProvince($p);

$c3->setProvince($p);

$p->setCity([$c1 , $c2 , $c3]);

//获取数据

echo $c1->getInfo();

echo $c2->getInfo();

echo $c3->getInfo();

echo $p->getInfo();

for( $i = 0; $i < count($p->getCity()); $i++ ) {

echo $p->getCity()[$i]->getInfo() . '-----';

}

var_dump($p);

function sum(int $a , int $b): float

{

return $a + $b;

}

var_dump(sum(1 , 2));

//-------------------------------

//class interface trait abstract

class Person{

private $namge;

private $age;

private $gender;

}

abstract class Player extends Person{

public abstract function learn();

}

abstract class Cocah extends Person{

public abstract function teach();

}

interface LearnEn{

public function learnEn();

}

trait LearnMath{

public function learnMath(){

echo "learn Math";

}

}

class BasketballPlayer extends Player implements LearnEn{

use LearnMath;//使用trait

public function learn(){

echo 'learn basketball';

}

//使用interface

public function learnEn(){

echo 'learn en';

}

}

class BasketballCocah extends Cocah implements LearnEn{

use LearnMath;//使用trait

public function teach(){

echo 'teach basketball';

}

//使用interface

public function learnEn(){

echo 'learn en';

}

}

$bbp = new BasketballPlayer();

$bbp->learn();

$bbp->learnEn();

$bbp->learnMath();

$bbt = new BasketballCocah();

$bbt->teach();

$bbt->learnEn();

$bbt->learnMath();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值