老师,是这样的。
在api接口模块的City控制器中有一个getCitysByParentId方法,因为我需要通过获取到的上级城市id去数据库中查询它对应的二级城市,所以实例化了app/common/model/City.php中的getNormalCitysByParentId方法,并传参$id
public function getCitysByParentId()
{
//获取前台提交的json数据
$id = input('post.id');
if(!$id){
$this->error('ID错误');
}
//根据上级城市id获取下一级的城市id
$citys = $this->obj->getNormalCitysByParentId($id);
if(!$citys){
return show(0,'error');
}
return show(1,'success',$citys);
}但是getNormalCitysByParentId方法的默认参数为$parentId=0
public function getNormalCitysByParentId($parentId=0){
$data = [
'status' => 1,
'parent_id' => $parentId,
];
$order = [
'id' => 'desc',
];
return $this->where($data)
->order($order)
->select();
}因为代码运行是正确的,所以我知道整个代码看起来像是$id的值替换了0,传递给了$parentId,但这是为什么呢?
我实例化了一个方法并传递了一个参数,那对应的方法中不也应该有一个相同的变量来接收这个参数才对吗?为什么$id直接将值赋给了$parentId呢?
望老师教我,感谢!