Cannot redeclare class - Check if class already exists

10,428

Solution 1

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 );

Solution 2

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.

Solution 3

If you cannot modify the way your class file is brought into the CMS (by using require_once or include_once), modify your snippet:

if( !class_exists( 'urlConverter' ) ) {
    class urlConverter {
        /* Build class */
    }
}

Of course if you have anything else in that same file, you'll want to make sure it doesn't try to run twice.

Solution 4

It looks like the file that defines the class is being included more than once which is why you are getting the first error (cannot redeclare class).

/var/www/core/cache/includes/elements/modsnippet/23.include.cache.php line 14 seems to be what is including the class multiple times. If possible, change the include to include_once so you don't define the class multiple times.

To debug further (instead of seeing the 500 internal server error), try adding the following to your code as early as possible:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Otherwise, check the servers error_log (Apache) and see if there is any useful information there about the 500 error.

Share:
10,428
Peter
Author by

Peter

Updated on June 16, 2022

Comments

  • Peter
    Peter almost 2 years

    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.

  • Peter
    Peter almost 12 years
    Yeah that seems about right. I don't know how to not do that. The CMS I'm working with does it automatically and I don't have much of a choice.
  • Sampson
    Sampson almost 12 years
    @Peter If you can't modify the CMS, modify your code to only create the class (not the instance) if it hasn't already been created previously.
  • Peter
    Peter almost 12 years
    Hmm.. That suppressed the error, but the second time I tried to get the output from the class it didn't return anything.
  • Peter
    Peter almost 12 years
    @JonathanSampson what do you mean?
  • Sampson
    Sampson almost 12 years
    @Peter Can you update your question with what you're presently trying? This should be the only place you call class_exists.
  • Peter
    Peter almost 12 years
    Oh I get it now! MODx caches classes to their registry using PDO. This works great.