我该如何在PhP中改进这种延迟加载模式?

static $searchQuery="nothing";

if ($searchQuery=="nothing")
{
    if ($referral=="")
    {
        $referral=referrer();
    }
    $searchQuery=getSearchQuery($referral);
}
return $searchQuery;

Basically I need to initialize $searchQuery with something.

I can initialize it with "" but the value of $searchQuery is often legitimately "".

So how should I initialize it? ""? Nil? Null? array()? What?

I can also use

static $result;
if  (isset($result))
{
    return $result;
}

but I got warning because $result is not defined.

It's up to you, but you need to be careful with PHP's loose typing. I prefer NULL, and I would write that code as:

static $searchQuery = NULL;

if( is_null($searchQuery) ) {
    if( emtpy($referral) ) {
        $referral=referrer();
    }
    $searchQuery=getSearchQuery($referral);
}
return $searchQuery;