码迷,mamicode.com
首页 > 移动开发 > 详细

Fatal error: Using $this when not in object context in C:\AppServ\www\ABC\model\Model.class.php on line 7

时间:2017-02-04 21:19:45      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:not   color   file   doc   service   不能   function   font   success   

原文转发:http://hi.baidu.com/zwnjiejie/blog/item/5e6555c23fa302120ff477dd.html

Fatal error Using $this when not in object context in D:\xampp\htdocs\test\php\service\FileCommand.php

大致意思是 $this 没有上下文,原因是没有对此类进行实例化。

出现此错误的原因是:在FileCommand.php中使用 $this->方法/属性。

$this 不是不可以用,而是要看情况用。在实例化的 类中使用 $this是可以的例:

class Person{

     private var    $name;

     private var    $sex;

    public function showName(){

          echo $this->name;

          $this->message();

   }

public function message(){

     echo "success";

}

}

如果是实例化 Person 类而直接访问的话就会出上面的错误,意思是 $this没有上下文。

正确用法:

   $person = new Person();

$person.showName();  

如果不想定义直接用的话,则可通过:   Person::message(); 注意里边不能含有 $this

Fatal error: Using $this when not in object context in C:\AppServ\www\ABC\model\Model.class.php on line 7

标签:not   color   file   doc   service   不能   function   font   success   

原文地址:http://www.cnblogs.com/gengyi/p/6366326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!