Using two classes with the same name

I have two PHP applications that have a twho class with the same name. - app1 with the class "Project" - app2 with the class "Project"

I need to use the classes of the first application in the second, but two classes with the same name cause an error ("PHP Fatal error: Can not redeclare class Project ...").

I cannot change class names. I have to use PHP 5.2 (no namespace in PHP 5.2).

Is there a solution?

May be:

  • use class Project
  • undef this class (view "unset Project", is this possible with PHP?)
  • include () 2nd class
  • use 2nd grade

I don't know if this is possible with PHP (don't find any source of information about this), and I don't know a better way to manage this ...

+3
3

, () Joomla. script, "version.php", . , , "JVersion", - () version.php.

, eval() require(). version.php , eval() , . , - . ...



    $v15_txt=file_get_contents($old_dir.'/libraries/joomla/version.php');
    $v15_txt=preg_replace('/class JVersion/i', 'class JVersion15', $v15_txt);
    eval('?>'.$v15_txt);
    $jvold=new JVersion15;
    $old_version = $jvold->RELEASE.'.'.$jvold->DEV_LEVEL;

    $v25_txt=file_get_contents($new_dir.'/libraries/cms/version/version.php');
    $v25_txt=preg_replace('/class JVersion/i', 'class JVersion25', $v25_txt);
    eval('?>'.$v25_txt);
    $jvnew=new JVersion25;
    $new_version = $jvnew->RELEASE.'.'.$jvnew->DEV_LEVEL;


, . , .: -)

, PHP "undeclare ($ classname)". namespacing, . .

+6

.

, . . , .

. . , .

, , , .

+4

In PHP 5.3 and PHP 7, an alias of the class that uses the script is possible. If app1\Projectthey app2\Projectare full class names, the code using them can use the following operators use.

use app1\Project;
use app2\Project as AppProject;

The rest of the code will be used AppProjectwhen referring to a class imported from the second line of code.

Link

0
source

Source: https://habr.com/ru/post/1782611/


All Articles