Import 2 classes with the same name

In PHP, I want to import 2 third-party classes with the same name. For instance:

// in file libs/3rdparty/AppleTree/Apple.php class Apple extends SomeOtherModel { // class details } // in file libs/3rdparty/AppleSeed/Apple.php class Apple extends SomeModel { // class details } 

Given that their constructor is the same, for example:

 $appletree = new Apple(); $appleseed = new Apple(); 

How can PHP differentiate two classes? In Java, we can add the class path before the constructor.

Note. I am using PHP 5 and I can’t modify classes because they are used by other class files.

+4
source share
4 answers

it is not the most beautiful and may not be the most effective, but it works. The concept is that instead of “including” the files, you read them as strings, wrap the namespace that you give it, and then evaluate it, and this allows you to call each function with the namespace ...

I need to add crazy details to algis for my code in In this example, I tweaked it to do what you wanted to do.

  function create_namespace($str, $namespace) { $php_start = 0; $tag = '<?php'; $endtag = '?>'; $start_pos = $php_start + strlen($tag); $end_pos = strpos($str, $endtag, $start_pos); $php_code = substr($str, $start_pos, $end_pos - $start_pos); if (strtolower(substr($php_code, 0, 3)) == 'php') $php_code = substr($php_code, 3); $part1 = "namespace $namespace ;"; ob_start(); eval($part1 . $php_code); ob_end_clean(); } $str1 = file_get_contents('apple1.php'); $str2 = file_get_contents('apple2.php'); create_namespace($str1, "red"); create_namespace($str2, "green"); $apple1=new red\apple(); $apple2=new green\apple(); echo $apple1->color(); echo "<br/>"; echo $apple2->color(); 

output:

 red green 
+4
source

You can use namespaces, but you cannot just include the file as the specified bkdude (although the idea is correct) you will need to add a namespace declaration at the beginning of those files that process classified So, something like the following

 #file1 namespace red{ class Apple { function me() { echo __CLASS__; } } } #file2 namespace green { class Apple { function me() { echo __CLASS__; } } } 

And include these files wherever you use them, then you can use them as follows.

 $apple1 = new red\Apple(); $apple2 = new green\Apple(); $apple1->me(); $apple2->me(); 
0
source

Use namespace. The syntax is as follows:

 file1.php <?php namespace "mynamespace"; class apple{ } ?> 

Currently, the apple class is only available in the my namespace namespace. Creating an instance of a class like this will not work:

 <?php require_once "file1.php" $apple = new apple(); ?> 

You will need to use a namespace so that PHP finds the class definition as follows:

 <?php require_once "file1.php" use mynamespace; $apple = new mynamespace\apple(); ?> 
0
source

I think you need to use namespaces for this

I am not an expert in namespaces, but I would suggest something like this.

 <?php namespace red { include (apple1.php); } namespace green { include (apple2.php) } ?> 

then call then $ nice = new red / apple () and $ rotten = new green / apple ()

-1
source

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


All Articles