durduvakis
durduvakis

Reputation: 189

php Cannot redeclare class / Undefined variable Joomla Module

I am experimenting with OOP yet, I am very new to it so, I have created a module for joomla 2.5.x and inside it's main php file I require_once a file with a class like this:

require_once(JPATH_SITE.'/modules/'.$module_dir.'/libs/classes.php');

classes.php contains this:

class experiment
{
    function clean($input)
    {
        return filter_var($input, FILTER_SANITIZE_STRING);
    }
}

$my = new experiment;

Then in the module php file I use it like:

$name = $my->clean($params->get('name'));

Until this point it all behaves normal. But that only when the module appears only once on the same page. If I duplicate the module and use it twice on the same page I get the error Undefined variable my. If I change the require_once to require, I get the error Cannot redeclare class experiment. Weird.

I have also tried if (!class_exists(.. or if(!isset($my)) { .. } but I knew it was not going to work.

I will appreciate any kind of help, cheers.

Upvotes: 1

Views: 595

Answers (1)

jcsanyi
jcsanyi

Reputation: 8174

When you include a file, any classes defined in that file are then available globally, but any variables are in the same scope as the include - in this case, just within the function that you originally included the file from.

As a result, when you're using require_once(), you're seeing that the second time you try to include it, nothing happens - which is normal, since the file was already included, but the $my variable is also not defined - because it went out of scope when the function ended the first time.

When you switch to require(), you get a different error, because you're redefining the class.


The solution here is to not set global variables when you're defining your class. Define the class - and then when you need to use it, create an instance of that class. Don't depend on a global variable.

If you're trying to only create one instance of the class (do you really need to do that?), look into using a singleton pattern instead.

Upvotes: 1

Related Questions