php7数组排序与trait使用

    排序在编程中是一个重要的东西,php7对数组的排序简化了不少,这是我自己测试过的,大家可以看看,由于有不少于一个实例,所以你们在用我的东西进行排序时,要先注释掉其余的部分,代码如下:

<?php
header("Content-type:text/html;charset=utf-8");
/**
 此文件为测试php7新特性
*/
//对数组进行排序
/**
$arr = array(
    array('fee'=>9680,'course'=>'struct'),
    array('fee'=>5680,'course'=>'php'),
    array('fee'=>1680,'course'=>'javascript'),
    array('fee'=>8680,'course'=>'c++'),
    array('fee'=>6680,'course'=>'java'),
    array('fee'=>3680,'course'=>'python'),
);
function user_sort($a,$b){
    if($a['fee']>$b['fee']){
        return true;
    }elseif($a['fee']<$b['fee']){
        return false;
    }else{
        return 0;
    }
}
usort($arr,'user_sort');
var_dump($arr);//按fee排序后的结果
**/
$db = mysqli_connect('localhost','root','','products');
$sql = 'select id,parent_id,product_name,addtime,updatetime from products where p_status=1 order by addtime';
$result = mysqli_query($db,$sql);
while(list($id,$parent_id,$product_name,$addtime,$updatetime)= mysqli_fetch_array($result,MYSQLI_NUM)){
    $products[]=array('id'=>$id,'parent_id'=>$parent_id,'product_name'=>$product_name,'addtime'=>$addtime,'updatetime'=>$updatetime);
    //任意组装数组,比如四维数组
};
/**
function product_sort($a,$b){
    return strcmp($a['product_name'],$b['product_name']);//根据名称排序
}
**/
//根据添加时间排序
/**
function addtime_sort($a,$b){
    if($a['addtime']>$b['addtime']){
        return false;
    }elseif($a['addtime']<$b['addtime']){
        return true;
    }else{
        return 0;
    }
}
**/
//根据修改时间排序
/**
function updatet_sort($a,$b){
    if($a['updatetime']>$b['updatetime']){
        return false;
    }elseif($a['updatetime']<$b['updatetime']){
        return true;
    }else{
        return 0;
    }
}
**/
//根据父类id排序
/**
function parentid_sort($a,$b){
    if($a['parent_id']>$b['parent_id']){
        return false;
    }elseif($a['parent_id']<$b['parent_id']){
        return true;
    }else{
        return 0;
    }
}
**/
//usort($products,'addtime_sort');//根据添加时间降序排序
//usort($products,'updatet_sort');//根据修改时间降序排序
//usort($products,'parentid_sort');//根据父类id降序排序

?>

以下是trait的使用方式

//测试trait
/**
trait tSomeTrait{
    function userRegister(){
        echo '测试trait';
    }
}
class Register{
    use tSomeTrait;
    function uppasswd(){
        return true;
    }
}
$obj = new Register();
$obj->userRegister();//输出测试trait
**/
trait tDebug{
    public function dumpObject(){
        $class = get_class($this);
        $attributes = get_object_vars($this);
        $methods = get_class_methods($this);
        echo '类的名字是:'.$class.'<br/>';
        echo '属性有:<br/>';
        foreach($attributes as $k=>$v){
            echo $k.'=>'.$v.'<br/>';
        }
        echo '方法有:<br/>';
        foreach($methods as $key=>$val){
            echo $val.'<br/>';
        }
    }
}
class UserInfo{
    use tDebug;
    public $width=0;
    public $height=0;
    function __construct($w=0,$h=0){
        $this->width=$w;
        $this->height=$h;
    }
    public function setSize($w=0,$h=0){
        $this->width=$w;
        $this->height=$h;
    }
    function getArea(){
        return ($this->width*$this->height);
    }
    function getperimeter(){
        return (($this->width+$this->height)*2);
    }
    private function isSqure(){
        if($this->width==$this->height){
            return true;
        }else{
            return false;
        }
    }
}
$obj = new UserInfo(3,5);
$obj->dumpObject();//trait的方法与类中方法同名,类中的方法优先级高于trait中的方法,若该类继承了另一个类,则trait中的方法高于父类方法。
//trait中可以有抽象方法,trait:主要是复用代码。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值