正在尝试获取php类中的登录用户数据

时间:2014-02-16 作者:user759235

我正在尝试获取当前登录的用户数据,并在父类中使用它(我在几个函数中需要它)。

在下面的示例中,我可以在第一个函数中获得userID,但在下一个函数中它不会给出任何输出。

这是我的第一个wordpress插件,也是我第一次使用OOP,谁能帮我解决这个问题呢。

<?php

class WP_PM{

    public $currentId;

    public function __construct(){

        $this->addActions();
        $this->showId();
    }

    private function addActions(){
        add_action(\'wp_loaded\', array(&$this, \'getId\'));
    }

    public function getId(){
        global $current_user;

        $this->currentId = $current_user->ID;

        //$this->currentId = \'hello\';//we have an output

        echo $this->currentId;//we have an output

        return $this->currentId;
    }

    public function showId(){
        echo $this->currentId;//no output
            echo $this->getId();//no output
    }

}

register_activation_hook(__FILE__, array(\'WP_PM\', \'activate\'));
register_deactivation_hook(__FILE__, array(\'WP_PM\', \'deactivate\'));

$wp_pm = new WP_PM();
?>
//基于G.M解决方案的新示例(仍不工作->触发wp错误,无登录文本)

class WP_PM_Extend extends WP_User {

    function getID() {
        return $this->ID;
    }

}

class WP_PM{

    protected $user;

    public function __construct(WP_PM_Extend $user = NULL){

        if (!is_null( $user ) && $user->exists()){
            $this->user = $user;
        }

        $this->getUser();

    }

    public function getUser() {
        return $this->user;
    }

}


function getWPPM() {

    if(!did_action(\'wp_loaded\')){
     $msg = \'Please call getCurrentUser after wp_loaded is fired.\';
    return new WP_Error(\'to_early_for_user\', $msg);
    }

    static $wp_powertour_core = NULL;

    if(is_null( $wp_powertour_core)){
        $wp_pM = new WP_PM( new WP_PM_Extend( get_current_user_id() ) );
    }

    return $wp_powertour_core;
}

function getCurrentUser(){

  $wppm = getWPPM();

    if(is_wp_error($wppm)){
        return $wppm;
     }

    $user = $wppm->getUser();

    if($user instanceof WP_PM_Extend){ 
        return $user;
    }
}

add_action( \'wp_loaded\', \'getCurrentUser\');

$current_user = getCurrentUser();
if($current_user instanceof WP_PM_Extend){
    echo \'Current user ID: \' . $current_user->getID();
}else{
    echo \'No one logged in\';
}

3 个回复
最合适的回答,由SO网友:gmazzap 整理而成

一旦你说你是第一次使用OOP,我想说:停止使用& 之前$this: PHP4早就去世了。

第二个提示,如果可以的话,不要使用全局变量。我知道这个函数wp_get_current_user 在内部使用全局变量,但我希望将来不会再这样了,不过我看不到global 代码中的单词,让我填充得更好一些。

第三个小贴士,尝试使用类型暗示使你的plguin更加坚实。

在这里,我发布了我是如何编写该代码的。

首先,我写了两个类,可能在两个不同的文件中,以相同的类命名:

class WP_PM_User extends WP_User {

  function getID() {
    return $this->ID;
  }

}

class WP_PM {

  protected $user;

  function __construct ( WP_PM_User $user = NULL) {
    if ( ! is_null( $user ) && $user->exists() ) $this->user = $user;
  }

  function getUser() {
    return $this->user;
  }

}
之后,在插件的某个地方(可能在主插件文件中),我会编写如下两个函数:

function getWPPM() {

  if ( ! did_action(\'wp_loaded\') ) {
    $msg = \'Please call getCurrentUser after wp_loaded is fired.\';
    return new WP_Error( \'to_early_for_user\', $msg );
  }

  static $wp_pm = NULL;

  if ( is_null( $wp_pm ) ) {
    $wp_pm = new WP_PM( new WP_PM_User( get_current_user_id() ) );
  }

  return $wp_pm;
}


function getCurrentUser() {

  $wppm = getWPPM();

  if ( is_wp_error( $wppm ) ) return $wppm;

  $user = $wppm->getUser();

  if ( $user instanceof WP_PM_User ) return $user;
}

add_action( \'wp_loaded\', \'getCurrentUser\' );
这样,在插件中的任何地方都可以调用getCurrentUser 并检索当前用户对象,如果需要ID,可以调用getID() 在返回的用户上getCurrentUser.

用法示例:

add_action( \'wp_loaded\', function() {

  $current_user = getCurrentUser();
  if ( $current_user instanceof WP_PM_User ) {
    echo \'Current user ID: \' . $current_user->getID();
  } else {
    echo \'No one logged in\';
  }

}, 30 );

SO网友:s_ha_dum

你两个都开枪了$this->getId();$this->showId(); 在构造函数中。我很确定这就是问题所在。

如果这是一个插件,因为您使用的是激活挂钩,所以看起来像是这样,那么您应该能够做到这一点:

public function getId(){
    global $current_user;
    get_currentuserinfo();

    $this->currentId = $current_user->ID;

    //$this->currentId = \'hello\';//we have an output

    echo $this->currentId;//we have an output

    return $this->currentId;
}
在那之后,一切都会好起来的。

如果在其他上下文中实例化,则需要注意它实例化的挂钩。太早了$current_user 也没有get_currentuserinfo() 将定义。

例如

add_action(
  \'muplugins_loaded\',
  function() {
    global $wp_pm;
    $wp_pm = new WP_PM();
  }
);
。。。将给您一个关于未定义函数的通知--get_currentuserinfo().

SO网友:Shazzad

您肯定是在提前加载类实例。

第一个解决方案可以这样加载类-

add_action( \'init\', \'load_wp_pm\' );
function load_wp_pm(){
    global $wp_pm;
    $wp_pm = new WP_PM();
}
另一种方法是通过修改__contruct 并添加另一种方法-

// modified the construct method
public function __construct(){
    add_action( \'init\', array( get_class(), \'_init\' ) );
}

// getting user id & setting the $currentId variable and 
public function _init(){
    $this->addActions();
    $this->showId();
}
这可能会显示一些输出。

结束

相关推荐

Several loop in search result

我想在搜索后的结果中使用两个循环。首先,如果有结果,我开始循环<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> 并在循环后显示属于某个类别的文章<?php $cats = get_categories(); foreach ($cats as $cat) { query_posts(\'cat=\'.$cat-&g