dongtao4319 2013-02-14 10:51
浏览 22
已采纳

($ foo)和(foo $ foo)之间的差异作为参数

could someone explain this to me?

What is the difference of these two methods?

function some_function($foo)
    {
    }

and

function some_function(foo $foo)
    {
    }

in my case

public function validate_something(notice $notice, $tok_id, $captcha) 
{
    if(something === true) {
        $notice->add('info', 'Info message');
    } else {
        $notice->add('error', 'Error message');
    }
}

$notice is a object of class notice

class notice {

    private $_notice = array();

    public function get_notice(){
        return $this->_notice;
    }

    public function add($type, $message) {
        $this->_notice[$type][] = $message;
    }
    ...
}
  • 写回答

2条回答 默认 最新

  • dongyizhui0616 2013-02-14 10:53
    关注

    It's called type hinting

    function some_function($foo)
    {
    }
    

    Expects an argument to be passed to some_function when it is called

    function some_function(bar $foo)
    {
    }
    

    Expects an argument of datatype (class, abstract or interface, or array) bar to be passed to some_function when it is called.... if you pass an argument that isn't of this datatype, you'll get catchable fatal error

    EDIT

    What is the value of type hinting?

    • Because your code doesn't then need to use is_a or instance_of to validate arguments.
    • Reduces coding for defensive coders.
    • Functions/Methods are self-documenting for the arguments.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动