简体   繁体   中英

Fatal Error cannot redeclare?

The following code is not working and giving the following error:

Fatal error: Cannot redeclare class shareCount in /customers/8/4/5/WEBSITE.com/httpd.www/wp-content/themes/default/includes/share-count.php on line 2

loop.php (this is from my loop code for per post)

<?
    require_once("counter/share-count.php");
    $obj = new shareCount(get_permalink());
    echo "Tweets: ".$obj->get_tweets();
    echo "<br>Facebook: ".$obj->get_fb(); 
    echo "<br>Google+: ".$obj->get_plusones();
?>

share-count.php (this is the file thats executable on request)

<?
class shareCount
{
    private $url, $timeout;
    function __construct($url, $timeout = 10)
    {
        $this->url     = rawurlencode($url);
        $this->timeout = $timeout;
    }

    function get_tweets()
    {
        $json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
        $json        = json_decode($json_string, true);
        return isset($json['count']) ? intval($json['count']) : 0;
    }

    function get_fb()
    {
        $json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $this->url);
        $json        = json_decode($json_string, true);
        return isset($json[0]['total_count']) ? intval($json[0]['total_count']) : 0;
    }

    function get_plusones()
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode($this->url) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-type: application/json'
        ));
        $curl_results = curl_exec($curl);
        curl_close($curl);
        $json = json_decode($curl_results, true);
        return isset($json[0]['result']['metadata']['globalCounts']['count']) ? intval($json[0]['result']['metadata']['globalCounts']['count']) : 0;
    }

    private function file_get_contents_curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        $cont = curl_exec($ch);
        if (curl_error($ch)) {
            die(curl_error($ch));
        }
        return $cont;
    }
}
?>

I didn't find the eroor, but you could move the require_once-statement outside of the loop. This could help.

There might another shareCount class that is getting auto-loaded or loaded outside of what you have shared

I would debug further by checking shareCount object in line 1 share-count.php.

If you cannot find the answer, you could take the following hack to turn a blind eye to a different underlying issue by doing the following in share-count.php

<?php 
if (class_exists('shareCount')) {
   return;
}

class shareCount {
....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM