$new = new controllers\Order\NewOrder(); $new->setForm(new OrderFormData()); $new->sayForm();
private $form;public function setForm(\OrderFormData $example){ $this->form = $example;
}public function sayForm(){ echo $this->form->say();
}Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这种写法叫type hint, 就是setForm方法声明, 我的第一个参数必须是OrderFormData的实例. 不然不给传. 写了type hint之后, 反射类扫描方法参数的时候, 就会自动帮你new OrderFormData了