web-dev-qa-db-ja.com

非静的メソッドJUserHelper :: getProfile()は静的に呼び出すべきではありません

古いコンポーネントのコードをリファクタリングしていて、この警告に遭遇しました:

Strict standards: Non-static method JUserHelper::getProfile() should not be called statically, assuming $this from incompatible context 

$ userProfile = JUserHelper :: getProfile($ userId);

警告の技術的な性質は理解していますが、getProfilestaticとして宣言されていないため、JUserHelperは抽象的であるため、インスタンス化できません。

奇妙なことに、クラス内の他のすべてのメソッドはstaticとして定義されています。 3.xと同様に、2.5ではバグのように見えますが、署名は異なります。

では、プロファイルを取得する適切な方法は何ですか?

5
Valentin Despa

それはバグです。

これは、J3開発中に13-Jul-2012 c7c372のプラットフォームアップデートの一部として修正されました。

プラットフォームの変更は、2012年7月3日の99b6ac PR1341で、最初にCMSトラッカーアイテム28633を調達した後に行われました。

バグ33717を提起し、J2.5に対するパッチを提供した@valentinの功績によるものです。

7
Peter Wiseman

はい、おそらくバージョン2.5のバグです。 JUserHelperのgetProfile()関数はjoomla 2.5ディストリビューションのどこでも使用されていないため、バグが無人になる可能性があると思います。今宣言されているとおりに使用するには、JUserHelper-クラスをサブクラス化する必要がありますが、これは実際には意味がありません。

4
jonasfh

これはバグで、静的がありません http://prntscr.com/3hl3t1

静的インプレースプリント

$userProfile = JUserHelper::getProfile(42);
print_r( $userProfile );

戻り値

http://prntscr.com/3hl3x7

とりあえず、関数をコピーして、このように使用できます

function MygetProfile($userId = 0)
{
    if ($userId == 0)
    {
        $user   = JFactory::getUser();
        $userId = $user->id;
    }

    // Get the dispatcher and load the user's plugins.
    $dispatcher = JDispatcher::getInstance();
    JPluginHelper::importPlugin('user');

    $data = new JObject;
    $data->id = $userId;

    // Trigger the data preparation event.
    $dispatcher->trigger('onContentPrepareData', array('com_users.profile', &$data));

    return $data;
}
$userProfile = MygetProfile(42);
print_r( $userProfile );
3
Dan