CakePHP - Cannot redeclare class

2.3k Views Asked by At

I have a simple class that I've added to the components folder called mixpanel.php. Within the file is:

<?php
class MetricsTracker {
    public $token;
    public $host = 'http://api.mixpanel.com/';
    public function __construct($token_string) {
        $this->token = $token_string;
    }
    function track($event, $properties=array()) {
        $params = array(
            'event' => $event,
            'properties' => $properties
            );

        if (!isset($params['properties']['token'])){
            $params['properties']['token'] = $this->token;
        }
        $url = $this->host . 'track/?data=' . base64_encode(json_encode($params));
        //you still need to run as a background process
        exec("curl '" . $url . "' >/dev/null 2>&1 &"); 
    }
}
?>

In users_controller.php I do:

require 'components/mixpanel.php';

however I'm getting an error:

Fatal error: Cannot redeclare class MetricsTracker in /Users/Hooman/Sites/askedout/app/controllers/components/mixpanel.php on line 11

Why is this happening? I do the same thing with a different php class and it works fine. This is very odd to me as I am not repeating the require definition anywhere. Please help, thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

In your component code, please change the code from

class MetricsTracker to

class MetricsTracker extends Object

In your controller code , please add the following code :

var $components=array('MetricsTracker');

instead of using require() function