If i've different version of same third-party library ( or class ) that have of course same namespaces and class names. Is there a way to include them in same project avoiding name collisions?
Another case for this issue happens when we've a modular project where components are developed separately . So we can have different modules that includes same external library file in their own folder, but of course when modules will be loaded , we have a class collision.
in this article Loading multiple versions of the same class
an user suggests to use this code:
namespace old {
include /lib/api-1.0/library.php;
}
namespace foo {
include /lib/api-2.0/library.php;
}
$oldlibary = new old\Library();
$newlibrary = new foo\Library();
but of course it doesn't work. Classes collides anyway because they are declared gobally instead of vars.
So..is there another solution that is not hand-edit all the namespace of libraries to include?
thanks in advance
In this tutorial I will show you how you can install multiple PHP versions, on a server running Apache. By. Jacob Sometimes you may need to run multiple versions of PHP on the same server in order to support outdated software. I had to install PHP7.4 alongside PHP8.0 myself recently to support Nextcloud, which does not yet support PHP8.
PHP include and require Statements It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script
Lastly, add all three folders into Windows Path variable under: Advanced system settings --> Environment Variables --> System variables. Windows restart might be required. Now you can call $ php --version in command line which will outputs PHP 7.4.XX (cli) ..
PHP include vs. require. The require statement is also used to include a file into the PHP code. However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:
This is a very common problem in modular systems. In PHP you could try to solve it with very bad hacks (eval, file_get_contents, str_replace), but you would loose a lot of usefull tooling support (backtrace, debugging, op code cache, precise error reporting, just to mention a few).
The usual way to approch these problems is to use some kind of dependency management. E.g. in java maven is used most times, in scala it is maven or sbt and php guys would normaly use composer (never used this one, so I'm not completely sure whether it is good or not and whether it even supports what you need).
These tools usualy have some kind of algorithm to detect and solve typical version conflicts. However this will only work if two conflicting versions are compatible (or at least the part you are using is compatible). They will either select one of the provided versions, select some version in between (sometimes very strange) or let you decide which version to use. But of course, they also cannot solve problems in which you will definitely require both versions within the same namespace.
Generally said, the easiest way is to allways use the same library or module version company wide or at least application wide. The tools above will help you with that.
Sorry, to not help you with your concrete problem, but I hope some of the provided keywords will be of some use for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With