一千萬個為什麽

搜索

不在對象上下文時使用$ this - 我正在使用最新版本的php和mysql

這是user.php:

    include("databse.php");//retrieving successfully first name and lastname from databse file into user.php
    class user
    {
      public $first_name;
      public $last_name;
      public static function full_name()
      {
          if(isset($this->first_name) && isset($this->last_name))
          {
            return $this->first_name . " " . $this->last_name;

          }
          else
           {
                return "";
           }
       }
    }

其他php文件,index.php:

   include(databse.php);
   include(user.php);
    $record = user::find_by_id(1);
    $object = new user();
    $object->id = $record['id'];
    $object->username = $record['username'];
    $object->password = $record['password'];
    $object->first_name = $record['first_name'];
    $object->last_name = $record['last_name'];
   //echo   $object->full_name();
    echo $object->id;// successfully print the id
    echo $object->username;//success fully print the username
    echo->$object->full_name();//**ERROR:Using $this when not in object context**

?>

最佳答案

你不能在靜態函數中使用 $ this 。請改用 self ::

轉載註明原文: 不在對象上下文時使用$ this - 我正在使用最新版本的php和mysql