首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >严格标准:非静态方法StreamComment::getCommentsHTML()不应该静态调用,假设$this来自不兼容的上下文

问严格标准:非静态方法StreamComment::getCommentsHTML()不应该静态调用,假设$this来自不兼容的上下文
EN

Stack Overflow用户
提问于 2014-04-02 18:17:53
回答 1查看 4.6K关注 0票数 0

我正在使用奥菲里亚(基于joomla的社交网络脚本),当我点击链接“显示所有评论”以查看链接下的所有已发布评论时,我会在第一个评论上方得到以下错误。这个函数可以工作,所以所有的注释都会扩展,我可以看到所有的注释,但是错误也在那里。

严格标准:非静态方法StreamComment::getCommentsHTML()不应该静态调用,假设$this来自第31行C:\Program中不兼容的上下文

以下是comment.php文件中的第31行:

代码语言:javascript
复制
public function  showall()
{
    $stream_id = JRequest::getVar('message_id');
    $html = StreamComment::getCommentsHTML($stream_id);
    header('Content-Type: text/html; charset=UTF-8');
    echo $html;
    exit;
}

我已经尝试禁用了我的php.ini中的所有错误报告选项,但是这个错误不断出现。我还尝试将上面的代码更改为"public静态函数showall()“,但是我得到了一个错误,即严格标准:非静态方法StreamComment::getCommentsHTML()不应该被称为静态。

我希望在此之前能提供任何帮助。

这里是完整的comment.php

代码语言:javascript
复制
<?php
/**
 * @version     1.0.0
 * @package     com_administrator
 * @copyright   Copyright (C) 2011 - 2013 Slashes & Dots Sdn Bhd. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Offiria Team
 */

// No direct access.
defined('_JEXEC') or die;

jimport('joomla.application.controller');

class StreamControllerComment extends JController
{
    /**
     *
     */     
    public function display($cachable = false, $urlparams = false){
        parent::display( null );
    }


    /**
     *  Return HTML of all
     */     
    /**public function  showall()
    {
        $stream_id = JRequest::getVar('message_id');
        $html = StreamComment::getCommentsHTML($stream_id);
        header('Content-Type: text/html; charset=UTF-8');
        echo $html;
        exit;
    }

    /**
     *  Add message
     */                            
    public function add()
    {
        $user   = JXFactory::getUser();

        // Store stream
        $comment    = JTable::getInstance( 'Comment' , 'StreamTable' );
        $message    = JTable::getInstance( 'Stream' , 'StreamTable' );
        $message->load(JRequest::getVar('stream_id'));
        $user_id = JRequest::getVar('anon', false) ? JUserHelper::getUserId('anon') : $user->id;

        // People need to be able to read the message to add comment
        if( !$user->authorise('stream.message.read', $message) ){
            // No reason this code would ever get here!
            exit;
        }

        $comment->bind( JRequest::get('POST', JREQUEST_ALLOWRAW) );

        $comment->raw = json_encode( JRequest::get('POST', JREQUEST_ALLOWRAW) );
        $comment->user_id = $user_id;
        $comment->group_id = $message->group_id;            
        $comment->store();

        // Update group stats, if it is a group message
        if( !empty($comment->group_id)){
            $group  = JTable::getInstance( 'Group' , 'StreamTable' );
            $group->load($comment->group_id);
            $group->setParam('last_comment', $comment->id);
            $group->store();
        }

        // Trigger Notification 
        StreamNotification::trigger( 'profile_post_comment', $comment );

        // If the updated date is set further than the current date, it is a pinned stream item and shouldn't be updated
        $now        = new JDate();
        $updated    = new JDate($message->updated);
        $preventUpdate = ($updated->toUnix() > $now->toUnix()); // Check pinned item status too? For now... naa

        // Update stream stats. Recalculate the count
        $this->_recalculateCommentCount($comment->stream_id, $preventUpdate);

        // Get the HTML code to append
        $tmpl = new StreamTemplate();
        header('Content-Type: text/html; charset=UTF-8');
        echo $tmpl->set('comment', $comment)->fetch('comment.item');
        exit;
    }

    /**
     * Delete a message
     */
    public function delete(){
        $my = JXFactory::getUser();
        $id = JRequest::getVar('comment_id');
        $oldComment = '';

        $comment    = JTable::getInstance( 'Comment' , 'StreamTable' );
        $comment->load($id);

        if( !$my->authorise('stream.comment.delete', $comment) ){
            // No reason this code would ever get here!
            exit;
        }
        $oldComment = $comment->comment;
        $comment->delete();
        $this->_recalculateCommentCount($comment->stream_id, true);

        // Send back the original comment if it is the last one
        $data = array();
        $data['comment'] = $oldComment;

        header('Content-Type: text/json');
        echo json_encode($data);
        exit;
    }

    private function _recalculateCommentCount($message_id, $preventUpdate = false)
    {
        // Update stream stats. Recalculate the count
        $model = StreamFactory::getModel('stream');
        $stream = JTable::getInstance('Stream', 'StreamTable');
        $stream->load( $message_id ) ;
        $stream->setParam('comment_count', $model->countComments(array('stream_id' => $stream->id)));
        $stream->store( $preventUpdate );
    }

    /**
     *  Like a comment
     */
    public function like()
    {
        $user = JXFactory::getUser();
        $comment_id = JRequest::getVar('comment_id');

        $comment = JTable::getInstance('Comment', 'StreamTable');
        $comment->load($comment_id);

        $comment->like();
        $comment->store(true);

        $data = array();
        $data['label'] = JText::_('COM_STREAM_UNLIKE_LABEL');
        $data['count'] = $comment->getLikeCount();

        header('Content-Type: text/json');
        echo json_encode($data);
        exit;
    }

    /**
     * Unlike a comment
     */
    public function unlike()
    {
        $user = JXFactory::getUser();
        $comment_id = JRequest::getVar('comment_id');

        $comment = JTable::getInstance('Comment', 'StreamTable');
        $comment->load($comment_id);
        $comment->unlike();

        $comment->store(true);

        $data = array();
        $data['label'] = JText::_('COM_STREAM_LIKE_LABEL');
        $data['count'] = $comment->getLikeCount();

        header('Content-Type: text/json');
        echo json_encode($data);
        exit;
    }

    /**
     * Get all current likes
     */
    public function showlikes()
    {
        $comment_id = JRequest::getVar('comment_id');
        $comment = JTable::getInstance('Comment', 'StreamTable');
        $comment->load($comment_id);

        $likes = ($comment->likes) ? explode(',', $comment->likes ) : null;
        $likeUsers = array();
        $likesHTML = '';
        if ($likes) {
            foreach ($likes as $key => $val) {
                $likeUsers[] = JXFactory::getUser($val)->name;
            }

            $likesHTML = implode(", ", $likeUsers);
            $likesHTML = JXString::isPlural(count($likeUsers)) ? JText::sprintf('COM_STREAM_LIKE_THIS_MANY_LIST', $likesHTML) : JText::sprintf('COM_STREAM_LIKE_THIS_LIST', $likesHTML);
        }

        $data['likes'] = $likesHTML;

        header('Content-Type: text/json');
        echo json_encode($data);
        exit;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-02 18:41:53

这里发生的情况是这个函数被静态地调用:

代码语言:javascript
复制
StreamComment::showall(); // Static Call

但是它被定义为一个非静态方法,这意味着PHP期望它被调用如下:

代码语言:javascript
复制
$obj = new StreamComment();
$obj->showall(); // Dynamic Call

如果这是调用该方法的唯一位置,或者是静态调用,则可以通过将该方法重新定义为静态来修复此问题:

代码语言:javascript
复制
public static function showall() // Added the "static" keyword
{
    // [..code..]
}

希望这有帮助:) x

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22819747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档