0

I have a main class which is including two other scripts. They both are using a class with the same name which I can't change because it's a library. The two classes with the same name can also have a different Version. When I include the class a second time I get the error that I'm redeclaring the class LicenceManager. My code:
/plugin-one/check_licence.php:

function plugin_one_isLicenceValid() {
    ...
    require "/plugin-one/libs/class.licence_manager.php";
    $licenceManager = new LicenceManager($path, $plugin_name);
    return $licenceManager->isValid();
}

/plugin-two/check_licence.php:

function plugin_two_isLicenceValid() {
    ...
    require "/plugin-two/libs/class.licence_manager.php";
    $licenceManager = new LicenceManager($path, $plugin_name);
    return $licenceManager->isValid();
}

main.php:

require '/plugins/plugin-one/check_licence.php';
$result = plugin_one_isLicenceValid();
...
require '/plugins/plugin-two/check_licence.php';
$result = plugin_two_isLicenceValid();

Is there a way to include the class a second time?

Why its not a duplicate?
Most existing questions seems to be able to edit the including class and add a namespace into it. I can't to that. I can only edit the two check_licence.php files and the main.php

TmCrafz
  • 184
  • 1
  • 12
  • 1
    You may want to look at `extending` your classes, as in https://stackoverflow.com/questions/11194506/php-extending-class – designosis Sep 20 '20 at 18:58
  • require_once, and/or wrap LicenceManager class in a class_exists call, you cant have 2 LicenceManager classes for each plugin, i.e different code if you dont use a namespace – Lawrence Cherone Sep 20 '20 at 18:58
  • 2
    This is why modern PHP libraries use namespaces. – ceejayoz Sep 20 '20 at 18:58
  • https://stackoverflow.com/questions/137006/redefine-class-methods-or-class has some ideas, but doesn't sound particularly reliable. – Nigel Ren Sep 20 '20 at 19:05
  • @ceejayoz when the library class would use a namespace, how would it solve the problem? I tested the example with a self created class and was putting it into a namespace. Now I get the Error ```Fatal error: Cannot declare class test_namespace\OwnLicenceManager, because the name is already in use``` – TmCrafz Sep 20 '20 at 20:29
  • @TmCrafz If there are two *different* license manager classes with the same names, they'd be in different namespaces. If two pieces of code are importing the *same* license manager, that'd be handled by something like Composer's autoloader. In either case, this is a solved problem and has been for years; if it's your code, you should update it to use modern standards. – ceejayoz Sep 20 '20 at 20:45

0 Answers0