1

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.

durduvakis
  • 189
  • 4
  • 16

1 Answers1

1

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.

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
  • wow ! haha I didn't think of taking the `$my = new experiment;` from inside the classes.php and putting it in the module file. I just did it thanks to your help and it made me laugh that it worked excellent ! thanks man I really appreciate :) it was THAT simple. – durduvakis Jul 14 '13 at 00:05
  • well, I have experimented with this for 2 reasons. 1, so that I am more sure that the function name will not be found again in all Joomla, and 2, of course to learn OOP slowly slowly... – durduvakis Jul 14 '13 at 00:10
  • Thank you man for this, I will try to learn about singleton pattern as well! – durduvakis Jul 14 '13 at 00:15
  • @durduvakis Just don't go too far overboard with singleton. It tends to be heavily overused - and there's often no real reason to restrict a class to just one instance. – jcsanyi Jul 14 '13 at 00:19
  • I hope the day that my eyes will open wide with OOP will come soon, cause I am still at a point that I most of the times don't understand what I see and reminds me of when I first saw php code :) – durduvakis Jul 14 '13 at 00:23