常见的php函数在hack中

Tik*_*ksi 7 hacklang

我决定开始一个新项目进入hacklang,并在修复了一些问题之后我最初遇到了从php习惯过渡的问题,我遇到了以下错误:

Unbound name: str_replace
Unbound name: empty
Run Code Online (Sandbox Code Playgroud)

做一些研究我发现这是因为使用了'遗留'的php,它不是类似于typecheck,并且会出错//strict.

这很好,所有,empty()很容易取代,但str_replace()有点困难.

是否有一个等效函数可以使用//strict?或至少类似的东西.

我知道我可以使用,//decl但我觉得在我的情况下失败了目的.

是否至少有任何方法可以告诉哪些函数在hack中实现,哪些不在文档中,因为我找不到?

作为参考(虽然它与问题本身不太相关),这里是代码:

<?hh //strict
class HackMarkdown {
    public function parse(string $content) : string {
        if($content===null){ 
            throw new RuntimeException('Empty Content');
        }
        $prepared = $this->prepare($content);

    }
    private function prepare(string $contentpre) : Vector<string>{
        $contentpre = str_replace(array("\r\n","\r"),"\n",$contentpre);

        //probably need more in here
        $prepared = Vector::fromArray(explode($contentpre,"\n"));
        //and here
        return $prepared;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您根本不需要更改代码.您只需要告诉Hack工具所有内置的PHP函数.

最简单的方法是下载此文件夹并将其放在项目中的某个位置.我把它放在我项目基础的hhi文件夹中.那里的文件告诉Hack所有内置的PHP函数.

他们中的大多数没有类型提示,这可能导致Hack认为一切的返回类型mixed而不是实际的返回,这在大多数情况下实际上是正确的,例如,str_replace可以返回a string或a bool.但是,它确实会停止"未绑定名称"错误,这是添加它们的主要原因.