我需要构建一个方法,将对象作为参数传递。此方法使用PHP“instanceof”快捷方式。
//Class is setting a coordinate.
class PointCartesien {
//PC props
private $x;
private $y;
//Constructor
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
//The method in question... It makes the coordinate rotate using (0,0) as default and $pc if set.
//Rotation
public function rotate($a, PointCartesien $pc) {
//Without $pc, throws error if empty.
if(!isset($pc)) {
$a_rad = deg2rad($a);
//Keep new variables
$x = $this->x * cos($a_rad) - $this->y * sin($a_rad);
$y = $this->x * sin($a_rad) - $this->y * cos($a_rad);
//Switch the instance's variable
$this->x = $x;
$this->y = $y;
return true;
} else {
//...
}
}
}
使用isset()会抛出错误。我希望它的工作方式是将$ pc参数rotate($ a,PointCartesien $ pc = SOMETHING)设置为默认值(0,0)。我该怎么做?
答案 0 :(得分:2)
您的函数调用需要$pc
参数,因此在进行isset()
检查之前,您会收到错误消息。尝试public function rotate($a, PointCartesien $pc = null) {
,然后使用is_null
检查代替isset
。