php5 中$this ,self,parent,static const

博客主要介绍了PHP中const、$this、self、static的运用与区别,通过代码示例展示了它们在类中的使用情况。同时还讲解了parent在扩展class上的应用,给出了相关代码示例,帮助理解其在继承中的作用。
关于php5 中$this ,self,parent,static const的使用,理清一下.const,$this,self static 运用与区别Code:

const,$this,self static 运用与区别

Code:

class foo{
 const TMP = 'This is const TMP';
 private static $tmp1 = 0;
 private $tmp2 = 1;
 function __construct()
 {
  print self::$tmp1;
  //print self::$tmp2; //error $tmp2 is not static
  //print $this->tmp1; //null $this->tmp1 is null,tmp1 is static
  print $this->tmp2;
  print self::TMP; //print 'this is const TMP'
 }
 
 static function test()
 {
  print "this is static fun";
 }
}

print foo::TMP; //print const TMP;
foo::test(); //print test() out;
$obj = new foo;
$obj->test(); //this is fun

parent 主要用在扩展class 上,

Code:

<?php
class foo{
 const TMP = 'This is const TMP';
 private static $tmp1 = 0;
 private $tmp2 = 1;
 function __construct()
 {
  print self::$tmp1;
  //print self::$tmp2; //error $tmp2 is not static
  //print $this->tmp1; //null $this->tmp1 is null,tmp1 is static
  print $this->tmp2;
  print self::TMP; //print 'this is const TMP'
 }
 
 static function test()
 {
  print "this is static fun";
 }
}

class foo2 extends foo{

 function __construct()
 {
  parent::__construct(); //__connstruct used
  parent::test(); //print static fun;
  print parent::TMP; //print const
  //print parent::tmp1; //error
 }
}
print foo::TMP; //print const TMP;
foo::test(); //print test() out;
$obj = new foo;
$obj->test(); //this is fun

print "/n Test parent /n";

$obj2 = new foo2();

?>¢

<?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、付费专栏及课程。

余额充值