输入banner图图片脚本导航/分类

状态模式,return无返回值的问题

代码如下:
我使用echo是有值的,为啥我使用return就获取不到值呢?

class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            $w->WriteCode();  
        }   
    }  
} 

回复内容:

代码如下:
我使用echo是有值的,为啥我使用return就获取不到值呢?

class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            $w->WriteCode();  
        }   
    }  
} 

谢谢邀请!因为你实例化掉用了本身,本身是return返回而不是输出之类的,所以不会出现值,这很正常!如果你直接echo掉用的就可以看到输出值了。

class GoodNight implements IState  
{ 
    public function WriteCode(Work $w)  
    {  
        if($w->hour<22)  
        {  
           return Yii::t('yii','Good night'); 
        }else{  
            $w->SetState(new GoodAtNight());  
            echo $w->WriteCode();  // 在这里echo就有值了
        }   
    }  
}