<?php
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();
php类、接口、抽象、trait、强类型使用
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- abstract class HeHe{ public $age=18;//可以定义属性public f...