关于self,static,$this的说明

关于self,static,$this的说明

在类中会看到 self,static,$this 调用属性和方法,但其实是有所区别的。

class test{
    public function info(){
        return $this;
    }
}

class test1 extends test{

}

var_dump((new test)->info());

/*
    class test#1 (0) {
    }
*/

var_dump((new test1)->info());

/*
    class test1#1 (0) {
    }
*/


1、$this 和 static 代表使用者,即例子中的 test1

	static可调用静态的属性和方法
	$this调用非静态的

2、self 仅表示定义者,即例子中的 test

	self可调用静态的属性和方法

3、new self 和 new static 也是同样的区别。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class WebModel extends Model { /** * Create a new Eloquent model instance. * * @param array $attributes */ public function __construct() { $this->setTable('pms_web'); parent::__construct(); } const STATUS_NORMAL = 1; //正常 const STATUS_FREEZE = 2; //冻结 public static function Enum($sign = "") { $status = [ 'status' => [ self::STATUS_NORMAL, self::STATUS_FREEZE ] ]; return isset($status[$sign]) ? $status[$sign] : $status; } public function WebDb() { return DB::table($this->getTable(),'w'); } public function FindOne($content, $param = "id") { $result = $this->WebDb()->where($param, $content)->select(DB::raw('id'))->first(); if (empty($result)) { return ''; } return $this->FormatOne($result); } public function FindList($params) { $query = $this->WebDb(); $query->select( DB::raw('SQL_CALC_FOUND_ROWS id') ); $query->limit($params['per_page']); $query->offset($params['offset']); $query->orderByDesc('w.id'); $result = $query->get(); $return_result = array( 'total' => 0, 'data' => [], ); if (empty($result)) { return $return_result; } $fromat_result = $this->FormatList($result); $total = DB::select("select FOUND_ROWS() as num")[0]->num; return [ 'total' => $total, 'data' => $fromat_result, ]; } public function FormatList(&$result) { foreach ($result as $v) { $this->FormatOne($v); } return $result; } public function FormatOne(&$result) { return $result; } public function simpleSave($data, $where = []) { } }
06-09

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值