简体   繁体   English

如何使用可选参数键入PHP函数

[英]How to type-infer PHP-functions with optional arguments

The thing with PHP is that it doesn't have a specific syntax when using a function with optional arguments. PHP的问题是,使用带有可选参数的函数时,它没有特定的语法。 This: 这个:

foo(10);

can either be this 可以是这个吗

function foo($a) {}

or this 或这个

function foo($a = 0) {}

or this 或这个

function foo($a, $b = 0, ...) {}

(or even function foo() {} , but disregard that for now). (甚至function foo() {} ,但暂时不考虑它)。

So how do we type-infer it when a function is used before it's defined? 那么,在定义函数之前使用函数时,如何进行类型推断呢?

One solution is to record all usages until the definition comes, and then see if they all can unify with it. 一种解决方案是记录所有用法,直到定义出现,然后查看它们是否都可以与之统一。 The example above would then store 然后,上面的示例将存储

int -> unit

and later check if it's compatible with 然后检查它是否与

int -> (typ list) -> unit

or whatever (where typ list is the list of optional arguments). 或其他任何内容(其中typ list是可选参数的列表)。

You think this could work? 您认为这可行吗? Are there other conventional ways to deal with this? 还有其他常规方法可以解决此问题吗?

I don't know to what degree this answers your question, but to elaborate on my comment above you could use PHP's built-in suite of reflection classes to help introspect and derive useful information about functions and their arguments. 我不知道这在多大程度上可以回答您的问题,但是要详细说明我在上面的评论,您可以使用PHP的内置反射类套件来帮助内省和获取有关函数及其参数的有用信息。

Specifically, I think ReflectionFunction and ReflectionParameter could be useful in your case, although there is much more functionality that might help. 具体来说,我认为ReflectionFunctionReflectionParameter在您的情况下可能会有用,尽管还有很多功能可能会有所帮助。

For example, here is a very simple example where you want to get some information about a function's arguments: 例如,这是一个非常简单的示例,您想在其中获得有关函数参数的一些信息:

<?php

// a simple function with an
// optional/default argument
function foo($a, $b = 0)
{
    return $a + $b;
}

// create a ReflectionFunction object
// passing the name of the function name.
// you can also pass a variable reference 
// to an anonymous function.
$refl = new ReflectionFunction('foo');

// iterate over the ReflectionFunction's
// parameter list to get a ReflectionParameter
// object for each of foo's arguments. here we're 
// just printing out a __toString() summary of each
// argument.
foreach ($refl->getParameters() as $param) {
    echo $param . PHP_EOL;
}

Yields: 产量:

Parameter #0 [ <required> $a ]
Parameter #1 [ <optional> $b = 0 ]

As you can see, this simple example shows some information about foo 's argument list: param number, required/optional, var name and default value. 如您所见,这个简单的示例显示了有关foo的参数列表的一些信息:参数编号,必需/可选,变量名和默认值。

$param is an instance of ReflectionParameter so there is a bunch of methods you can call on that to get more information about each argument, including: $paramReflectionParameter一个实例,因此可以调用一堆方法来获取有关每个参数的更多信息,包括:

export
__construct
__toString
getName
isPassedByReference
canBePassedByValue
getDeclaringFunction
getDeclaringClass
getClass
isArray
isCallable
allowsNull
getPosition
isOptional
isDefaultValueAvailable
getDefaultValue
isDefaultValueConstant
getDefaultValueConstantName

Hope this helps! 希望这可以帮助! :) :)

您可能必须插入“弱”类型变量并保留它们,直到找到函数定义为止,或者首先构建完整的AST,然后通过类型推断对其进行运行,如果存在定义不明的函数,则会引发错误(我想知道您如何处理内置函数,也许使用预烘焙的类型签名数据库?)。

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

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