PHP“重载”方法签名的歧义消除

发布于 2024-12-02 08:57:16 字数 379 浏览 1 评论 0原文

我有一个通过利用 func_get_args 来确定方法签名来支持复杂“重载”的方法。然而,在某些情况下,参数类型太模糊而无法区分。

designGarment('my shirt', SIZES::XXL, FABRICS::COTTON);
designGarment('my dress', FABRICS::SILK, ATTIRES::PARTY);

在上面的示例中,两个方法签名都解析为 STRING、INT、INT,因为 SIZES、FABRICS 和 ATTIRES 是为其各自属性定义了整数常量的类。我希望能够区分 (STRING, SIZES, FABRICS) 签名和 (STRING, FABRICS, ATTIRES) 签名。这在 PHP 中可能吗?

I have a method that supports complex "overloading" by utilizing func_get_args to determine the method signature. In some situations however, the argument types are too ambiguous to make a distinction.

designGarment('my shirt', SIZES::XXL, FABRICS::COTTON);
designGarment('my dress', FABRICS::SILK, ATTIRES::PARTY);

In the example above, both method signatures resolve to STRING, INT, INT because SIZES, FABRICS and ATTIRES are classes with integer constants defined for their respective properties. I want to be able to distinguish a (STRING, SIZES, FABRICS) signature from a (STRING, FABRICS, ATTIRES) signature. Is this possible in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2024-12-09 08:57:16

使用对象而不是猜测参数:

class Garment
{
  var $name;
  var $size;
  var $fabric;
  var $attires;
}

$Garment = new Garment(...);
function designGarment($Garment);

或者使用键/值对数组来显式指定参数及其值:

designGarment(array(
  'name' => 'my dress',
  'fabric' => FABRICS::SILK,
  'attires' => ATTIRES::PARTY
));

Use objects instead of guessing arguments:

class Garment
{
  var $name;
  var $size;
  var $fabric;
  var $attires;
}

$Garment = new Garment(...);
function designGarment($Garment);

Or use an array of key/value pairs to explicitly specify arguments and their values:

designGarment(array(
  'name' => 'my dress',
  'fabric' => FABRICS::SILK,
  'attires' => ATTIRES::PARTY
));
我恋#小黄人 2024-12-09 08:57:16

除了@Brad Christie 的答案之外,还有其他一些:

  1. 推荐:按常量顺序使用参数,并使用 null 作为缺失参数的默认值

    function designGarment($name, $size = null, $fabric = null, $attire = null){
        if(!is_null($size)){ }
        //ETC
    }
    
    designGarment('我的衣服', NULL, FABRICS::SILK, ATTIRES::PARTY);
    
  2. 使用对象存储可选参数

    $options = new stdClass;
    $options->size = SIZES::XXL; 
    函数 designGarment($name, $options = null){
    
    }
    
  3. 为每种类型的属性创建单独的对象

    函数设计服装(){
        foreach(func_get_args() as $arg){
            if($arg实例大小){ }
        }
    }
    designGarment($name, new Size('XXL'), new Fabric('WOOL'));
    
  4. 与上面类似,但为每种类型和属性值创建单独的对象(不推荐,但我见过一些使用此方法的情况)< /p>

    类大小{ public $size; }
    类 SizeXXL 扩展 Size{
        公共函数 __construct(){ $this->size = SIZES::XXL; } 
    }
    designGarment($name, new SizeXXL);
    

Beside @Brad Christie answer there are few others:

  1. Recomended: use arguments in constant order and null as default values for missing ones

    function designGarment($name, $size = null, $fabric = null, $attire = null){
        if(!is_null($size)){ }
        //etc
    }
    
    designGarment('my dress', NULL, FABRICS::SILK, ATTIRES::PARTY);
    
  2. Use Object to store optional arguments

    $options = new stdClass;
    $options->size = SIZES::XXL; 
    function designGarment($name, $options = null){
    
    }
    
  3. Make separeate object for every type of property

    function designGarment(){
        foreach(func_get_args() as $arg){
            if($arg instanceof Size){ }
        }
    }
    designGarment($name, new Size('XXL'), new Fabric('WOOL'));
    
  4. Similar to above, but to have separate object for every type and value of property (not recommended, but I've seen some cases using this)

    class Size{ public $size; }
    class SizeXXL extends Size{
        public function __construct(){ $this->size = SIZES::XXL; } 
    }
    designGarment($name, new SizeXXL);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文