PHP: Dealing with function case-insensitivity
44 观看
2回复
84 作者的声誉
I have a situation where there are similar functions in different classes, let's call them "Execute" and "execute."
class oldVersion {
public function Execute($x){
echo 'This does the same thing as newVersion->execute(1)';
echo 'but the class contains a lot of garbage that PHP7 doesn\'t like.';
echo 'and the app dies if it\'s included';
}
}
class newVersion {
public function execute($x, $y = 0, $z = 0){
if($y == 1){
echo 'do thing one';
} else {
echo 'do thing zero';
}
return 'bar';
}
}
$obj1 = new oldVersion();
$obj1->Execute('foo');
$obj2 = new newVersion();
$obj2->execute('bar', 1);
The class oldVersion has a lot of things wrong with it and won't function at all under PHP7, so what I'd really love to be able to do is move Execute into newVersion and do this:
class oldVersion_deprecated {
// no longer included so PHP7 doesn't break
}
class newVersion {
public function Execute($x){
return $this->execute($x, 1);
}
public function execute($x, $y = 0, $z = 0){
if($y == 1){
echo 'do thing one';
} else {
echo 'do thing two';
}
return 'bar';
}
}
$obj1 = new newVersion();
$obj1->Execute('foo');
$obj2 = new newVersion();
$obj2->execute('bar', 1);
But naturally I get
FATAL ERROR: cannot redefine execute
Is there a fancy workaround, or am I stuck finding and rewriting every call?
作者: Bill in Kansas City 的来源 发布者: 2017 年 12 月 27 日回应 2
1像
29 作者的声誉
Just remove the first execute function that calls the "real" one. Functions are case insensitive so you dont have to have two
class newVersion {
public function execute($x, $y = 0, $z = 0){
if($y == 1){
echo 'do thing one';
} else {
echo 'do thing two';
}
return 'bar';
}
}
/* All the same */
echo newv->Execute('foo');
echo newv->ExEcUTe('foo');
echo newv->execute('bar', 1);
作者: Joshua
发布者: 2017 年 12 月 27 日
1像
12693 作者的声誉
It's a bit messy, and I wouldn't normally recommend it, but you can simulate case-sensitive methods with PHP's "magic" __call
method. This gets called whenever a method can't be found in the given class. You can then check the supplied method name, and run whatever logic is appropriate.
class Foo
{
public function __call($name, $args)
{
switch ($name) {
case 'Execute':
return $this->oldExecute(...$args);
case 'execute':
return $this->newExecute(...$args);
}
}
private function oldExecute($x)
{
echo 'Running old function with arg: ', $x, PHP_EOL;
}
private function newExecute($x, $y, $z)
{
echo 'Running new function with args: ', implode(',', [$x, $y, $z]), PHP_EOL;
}
}
作者: iainn
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- php 你如何调试PHP脚本?
- php 在Htdocs之外创建XAMPP / Apache服务文件
- php 如何包含需要绝对路径的PHP文件?
- php 带隐藏按钮的登录脚本
- php 如何在PHP项目中找到未使用的函数
- php 在PHP中高效调整JPEG图像大小
- class 什么时候应该在C ++中使用类vs结构?
- class Python中旧样式类与新样式类有什么区别?
- class 如何在Java中创建对象的深层副本?
- class 静态类变量是否可能?
- class C ++中的struct和class有什么区别?
- class 如何删除类似const和非const成员函数之间的代码重复?
- case-sensitive 如何确定文件系统在.net中是否区分大小写?
- case-sensitive 不重新编译的不区分大小写的正则表达式?
- case-sensitive indexOf区分大小写?
- case-sensitive 区分大小写的文件系统上的不区分大小写的File.equals
- case-sensitive 我可以在CSS类名称中使用驼峰式大小写吗
- case-sensitive 如何在Java中反转String的大小写?