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
- 便宜 $3.5 月 2 核 CPU 1G 内存 25G SSD 1T 流全球[VPS测评]
- VULTR→按小时计费 KVM 月费 3.5 美元起 新用户送 100 全球[VPS测评]
- VM.DOG→$50 月 2GB 内存 60GB 空间 不限流量 1Gb虚拟空间(主机)
- js 错误→xx is not defined at HTMLAncho全球[VPS测评]
- php 中方法和函数的区别全球[VPS测评]
- 禁止 layer.msg()在回调时抖动全球[VPS测评]
- layer .tips 没有回调函数怎么办?全球[VPS测评]
- imidc→(香港+台湾)独立服务器 $30 月 e3-12xx 16g独立服务器[U]
- 六六云香港三网 CN2 GIA VPS 速度及综合性能测评,七折优惠,月香港VPS[主机]
- 惠工会 开通建行电子钱包 2 分钱充 20 元话费全球[VPS测评]
- 促销 iON→圣何塞线路改善 8 折优惠一个月 1 核 2G 50G S全球[VPS测评]
- 做站必选 腾讯云秒杀→2 核 4G 内存 50G 硬盘 3Mbps KV全球[VPS测评]
- ZJI 新春 55 折→香港大埔 2XE5-2630L 32GB 1TB香港VPS[主机]
- 给WordPress博客添加一个搜索引擎蜘蛛爬行及分析页面全球[VPS测评]
- WordPress 超高压缩率 支持webp图片格式的图片压缩插件:Sh全球[VPS测评]
- begin主题添加《英雄榜》每评论一次你就会自动排第一代码全球[VPS测评]
- Greencloudvps 绿帽云大硬盘存储VPS,最低100G硬盘,年全球[VPS测评]
- LightCloud 2019 新春: 终身折扣 VDS折扣 充全球[VPS测评]
- 阿里行癫:如何理解当下的云计算全球[VPS测评]
- 华为S2300 系列交换机如何修改密码?全球[VPS测评]
- 阳帝云:香港大宽带vps仅15元/月;美国vps200G防御仅28元/月美国VPS[主机]
- LOCVPS,全新香港大浦CN2 VPS 2核4G内存3M仅54/月香港VPS[主机]
- 如何设置指定路径跳转指定域名?全球[VPS测评]
- 葫芦云:香港将军澳CN2云服务器24元/月,1核/1GB/25G硬盘/3香港VPS[主机]
- 者也vps年终特惠:全场云服务器7折终身优惠,赠1G内存,充1000送3全球[VPS测评]
- 购买香港服务器需要做哪些准备香港VPS[主机]
- 硅云香港服务器怎么样?看网友们是怎么评价的香港VPS[主机]
- 六一云:春季大欢庆八折,香港CN2/美国高防/美国Cera,CDN产品,美国VPS[主机]
- MoeCloud:香港BGP云服务器,512MB内存/10GB空间/1T虚拟空间(主机)
- 春天的风最容易温暖心灵,适合朋友圈和抖音的12条文案全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-214152.html











