Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot redeclare class - Check if class already exists

Tags:

oop

php

class

I'm working with a script that is calling the same class twice and throwing the error:

Fatal: Cannot redeclare class urlConverter (/var/www/core/cache/includes/elements/modsnippet/23.include.cache.php:14)

I have tried placing the following code in:

if( !class_exists( 'urlConverter', false ) )
{
    $urlConverter = new urlConverter( $modx );
}

However doing that the CMS I'm working with is reporting an Error 500 and haven't been able to see in the logs why it's throwing that error.

Does anyone know how to check if that class has already been declared correctly?

Edit:

I'm using a CMS so as a result the Class is housed in a Snippet and not an actual file. This is how they call their snippets:

$data['viewonlinelink'] = $this->modx->runSnippet( 'urlConverter', array(
                                            'action' => 'encrypt',
                                            'string' => http_build_query( $string ) ) );

I need to call that a second time to get a different result.

Edit2:

Here is urlConverter:

<?php
class urlConverter {

public $modx;

public function __construct( modX &$modx )
{
    $this->modx =& $modx;
}


public function action( $scriptProperties )
{
    return $this->$scriptProperties['action']( $scriptProperties['string'] );
}

private function encrypt( $str )
{
    return $str;
}


private function decrypt( $str )
{
      return $str;
}


}
}
 $urlConverter = new urlConverter( $modx );
 return $urlConverter->action( $scriptProperties );

Now from another script I call the following:

    $data['viewonlinelink'] = $this->modx->runSnippet( 'urlConverter', array(
                                            'action' => 'encrypt',
                                            'string' => http_build_query( $string ) ) );
    $data['confirmonline']  = $this->modx->runSnippet( 'urlConverter', array(
                                            'action' => 'encrypt',
                                            'string' => http_build_query( $reversed ) ) );

Which runs the function encrypt inside of my urlConverter class and I should receive two different results.

like image 914
Peter Avatar asked Nov 30 '22 23:11

Peter


2 Answers

In using your updated code, change the class file to this:

<?php
if(!class_exists('urlConverter')){
    class urlConverter {

        public $modx;

        public function __construct( modX &$modx ){
            $this->modx =& $modx;
        }
        public function action( $scriptProperties ){
            return $this->$scriptProperties['action']( $scriptProperties['string'] );
        }

        private function encrypt( $str ){
            return $str;
        }


        private function decrypt( $str ){
          return $str;
        }

    }
 }
$urlConverter = new urlConverter( $modx );
return $urlConverter->action( $scriptProperties );
like image 64
Tim Withers Avatar answered Dec 04 '22 03:12

Tim Withers


The redeclare class error is not caused by creating a new instance of the class, it's called by invoking the class operator on the same symbol. You're probably including the urlConverter class definition file multiple times.

like image 24
Explosion Pills Avatar answered Dec 04 '22 02:12

Explosion Pills