donglue1886 2015-10-20 20:58
浏览 56
已采纳

在php中无法在静态方法中使用self

I can't use self in a static method , it gives me this error message : Fatal error: Using $this when not in object context in C:\xampp\htdocs\wordpress\wp-content\plugins\dw-usercp\usercp.php on line 136

here is the source code :

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' ) );

how do i call the create_plugin_pages() correctly then?

the plugin_activated() has to be static as Wordpress says

  • 写回答

3条回答 默认 最新

  • doutiao2540 2015-10-20 21:04
    关注

    Inside a static function, you're not in an instance of the class. You could:

    • instantiate an instance of the class and call the function
    • Pass an instatiated object into the static funtion
    • Make the create_plugin_pagesfunction static and call it with static.
    • Convert plugin_activated to not be static (MY VOTE)

    The static option won't work though since you call $this inside create_plugin_pages. So you'll need to go the instatiation route.

    Non-static

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

    Here's the passing in an object version

       public static function plugin_activated(dw_usercp $a_dw_usercp) {
           $a_dw_usercp->create_plugin_pages();
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算