简体   繁体   English

PHP:处理不区分大小写的函数

[英]PHP: Dealing with function case-insensitivity

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: oldVersion类有很多问题,在PHP7下根本无法运行,因此我真正想做的就是将Execute移到newVersion中并执行以下操作:

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? 有没有好的解决方法,还是我坚持寻找并重写每个电话?

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);

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. 这有点混乱,我通常不推荐这样做,但是您可以使用PHP的“魔术” __call方法模拟区分大小写的方法。 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;
    }
}

See https://eval.in/926262 参见https://eval.in/926262

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

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