2015-10-20 78 views
0

我不能在靜態方法使用self,它給了我這個錯誤消息: Fatal error: Using $this when not in object context in C:\xampp\htdocs\wordpress\wp-content\plugins\dw-usercp\usercp.php on line 136不能使用自己的靜態方法在PHP

這裏是源代碼:

class dw_usercp 
{ 
    public static function plugin_activated() { 
     self::create_plugin_pages(); 
    } 

    public function create_plugin_pages() { 
     $pages = array(
      'signin' => array(
       'title' => __('Sign In', 'dw-usercp'), 
       'content' => '[dwusercp-sigin-form]', 
       'option_id' => 'login_page' 
      ), 
      'user-account' => array(
       'title' => __('Your Account', 'dw-usercp'), 
       'content' => '[dwusercp-info]', 
       'option_id' => 'user_account_page' 
      ), 
      'edit-user-info' => array(
       'title' => __('Edit User Info', 'dw-usercp'), 
       'content' => '[dwusercp-edit-info]', 
       'option_id' => 'user_editinfo_page' 
      ), 
      'profile' => array(
       'title' => __('User profile', 'dw-usercp'), 
       'content' => '[dwusercp-profile]', 
       'option_id' => 'profile_page' 
      ), 
      'signup' => array(
       'title' => __('Sign Up', 'dw-usercp'), 
       'content' => '[dwusercp-signup-form]', 
       'option_id' => 'register_page' 
      ), 
      'user-lost-password' => array(
       'title' => __('Forgot Your Password?', 'dw-usercp'), 
       'content' => '[dwusercp-password-lost-form]', 
       'option_id' => 'lost_password_page' 
      ), 
      'user-password-reset' => array(
       'title' => __('Pick a New Password', 'dw-usercp'), 
       'content' => '[dwusercp-password-reset-form]', 
       'option_id' => 'password_reset_page' 
      ) 
     ); 

     foreach($pages as $slug => $page) { 
      $query = new WP_Query('pagename=' . $slug); 
      if (! $query->have_posts()) { 
       // Add the page using the data from the array above 
       $post_id = wp_insert_post(
        array(
         'post_content' => $page['content'], 
         'post_name'  => $slug, 
         'post_title'  => $page['title'], 
         'post_status' => 'publish', 
         'post_type'  => 'page', 
         'ping_status' => 'closed', 
         'comment_status' => 'closed', 
        ) 
       ); 

       $this->update_plugin_option($page['option_id'], $post_id); // this is the line 136 that the error message says 
      } 
     } 
    } 

    /** 
    * Update plugin option 
    * 
    * @param string $field option id 
    * @param mixed $value option new value 
    * @return bool 
    */ 
    public function update_plugin_option($field, $value) { 
     $options = get_option("dw_usercp_options"); 
     $options[$field] = $value; 

     update_option("dw_usercp_options", $options); 
    } 
} 
$dw_usercp = new dw_usercp(); 

register_activation_hook(__FILE__, array('dw_usercp', 'plugin_activated')); 

那我該如何正確調用create_plugin_pages()

plugin_activated()必須是靜態的WordPress的說

+2

我不禁笑了(我知道這不是建設性的)。你正在使用'$ this'。該錯誤告訴你,**不能在靜態上下文中使用$ this **。讓我們看看 - 下一步可能會是什麼?不能使用'$ this',但是你使用'$ this',但是你在標題中給自己一個答案:)這是另一個有問題和尋求幫助的水平,我還沒有看到很多這些。 –

+0

這個問題是由於使用'self'引起的,你還想調用靜態方法中的當前類的屬性或方法嗎? ,所以標題是正確的錯誤消息與這個問題的目的無關,問題是我們如何在靜態函數中調用非靜態函數,當我們不能使用'self'時,感謝@Ray,我得到了我的答案 – Amin

+0

噢,我不想摔死一匹死馬,但它真的很有趣,你幾乎得到了答案。有時候讀錯誤信息50次確實有助於你自己完成一些東西(這可以打敗某人告訴你答案是萬億次)。無論如何,祝你進一步的編程挑戰:) –

回答

2

裏面一個靜態函數,你的類的實例不是。你可以:

  • 實例化的類的實例,並調用該函數
  • 傳遞一個instatiated對象入靜funtion
  • 充分利用create_plugin_pages功能靜態和靜態調用它。
  • 轉換plugin_activated不是靜態的(我的選票)

,因爲你叫$thiscreate_plugin_pages靜態的選擇,雖然不會工作。所以你需要去安裝路線。

非靜態

public function plugin_activated() { 
     $this->create_plugin_pages(); 
    } 

下面是一個對象版本

public static function plugin_activated(dw_usercp $a_dw_usercp) { 
     $a_dw_usercp->create_plugin_pages(); 
    } 
2

它的流逝,因爲使用的是「$這個」變量當你在靜態情況下是(你看了?錯誤消息)

在靜態上下文時,使用方法:

self::method(args);

self::$attr變量(屬性)

1
$this->update_plugin_option($page['option_id'], $post_id); 

self,在您的標題引用。你應該使用:

self::update_plugin_option($page['option_id'], $post_id); 
相關問題