简体   繁体   English

php,版本控制和使用声明

[英]php, version control and use statements

Is it possible to use variable in use statement? 是否可以在use语句中使用变量? How to implement version control with traits and other files i define with use statement? 如何使用我使用use语句定义的特征和其他文件来实现版本控制?

<?php
namespace SomeNamespace;

$vers = '10'; 

use SomeNamespace2\someTrait_$vers;
use SomeNamespace3\someTrait_$vers;

I would like to be able to assign single version to all use statements. 我希望能够将单个版本分配给所有u​​se语句。

@Nick's answer is a good option. @Nick的答案是一个不错的选择。 But class_alias might be a better fit. 但是class_alias可能更合适。 (Or worse, depending on your situation or preferences...) Something like: (或更糟的是,取决于您的情况或偏好...)类似:

class_alias("SomeNamespace2\\someTrait_$vers", 'SomeNamespace2\someTrait');

and then just reference SomeNamespace2\\someTrait in the rest of your code. 然后只需在其余代码中引用SomeNamespace2\\someTrait

Although PHP has no support for variables in use statements, you could overcome that by putting your use statements (and other version specific code) into a series of include files (one for each version) and then use a variable in a require (or include ) statement to include the file you want. 尽管PHP不支持use语句中的变量,但是您可以通过将use语句(和其他版本特定的代码)放入一系列包含文件(每个版本一个),然后在require (或include )中使用变量来克服这一问题。 )语句以包含所需的文件。 For example: 例如:

use_10.php: use_10.php:

use SomeNamespace2\someTrait_10;
use SomeNamespace3\someTrait_10;

use_11.php: use_11.php:

use SomeNamespace2\someTrait_11;
use SomeNamespace3\someTrait_11;

Your main PHP code: 您的主要PHP代码:

$vers = 10;   // or 11, or any value for which you have created an include file
require_once "use_$vers.php";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM