[不花钱站长]:从免费域名开始[0元] Oracle永久免费VPS[0元] VPS环境搭建免费脚本[0元] 秒变大盘挂载免费网盘[0元] 小白免费采集器1天500W[0元] CF免费DNS+CDN服务[0元]
[零基础网赚]:撸本站免费源码[0元] 网站强引免费蜘蛛法[0元] 图片视频文件免费存储[0元] 外链轻松发布免费友链[0元] 入坑免费教程学习源代码[0元] 网上百种+赚钱联盟推荐[?元]

↑↑VPS推荐网 WWW.VPS.COM.IN 稳定 便宜 免备案↑↑


您现在的位置是:首页 > 全球[VPS测评] >  charges,context,getcharges,id,major,object,php,student,studentch,国外永久免费VPS

Using $this when not in object context错误原因及解决办法

全球[VPS测评]来源:VPS推荐网点击:915
【性价之王】【线路之王】【价格之王】【配置之王】
【免费之王】【香港首推】【梯子之王】【独服之王】
Using $this when not in object context错误原因及解决办法在php中出现Using $this when not in object context的原因是在静态方法中使用$this或者直接调用非静态的方法。

错误代码1:

//thinkphp 模型类 class StudentCharge extends Model { public static function getCharges($id) { $charges = $this->where('major_id',$student->major_id)->select(); return $charges; } } //调用 $charges = StudentCharge::getCharges($student->id);

以上代码就会报Using $this when not in object context错误。

解决办法1:在静态方法中使用self::来代替$this->,将上面代码的第5行修改为下面内容即可。

$charges = self::where('major_id',$student->major_id)->select();

解决办法2:实例化类后再调用该方法,将上面的调用代码修改为下面方式即可。

$sc = new StudentCharge; $charges = $sc->getCharges($student->id);

猜你可能想看的VPS


转载请注明原文地址:http://140.238.13.167:12355/read-157595.html

下一篇       上一篇