PHP 中具有可变参数列表的抽象方法
Abstract method with variable list of arguments in PHP
我在 PHP 中遇到了一个 OOP 问题。我试图实现一个抽象的 parent class 方法,并且从 child class 开始,我必须将它与可变数量的参数一起使用。
这是抛出的错误:
PHP Fatal error: Declaration of Square::getArea($length) must be compatible with Shape::getArea()
和 classes :
abstract class Shape {
abstract protected function getArea();
}
class Square extends Shape {
public function getArea($length)
{
return pow($length, 2);
}
}
class Triangle extends Shape {
public function getArea($base, $height)
{
return .5 * $base * $height;
}
}
我可以使用 child 的 __construct()
方法在启动时设置不同形状的属性,但我想知道是否存在其他方法并允许我定义可变参数列表。
提前致谢。
我认为使用 __construct
的想法确实是最好的方法。这就是它的用途。您希望每个形状都不同,并且每个形状都必须以不同方式计算面积。因此多态性和 OOP 设计原则。
话虽如此,但总有其他黑客攻击。我不推荐这种方法,但您可以根据需要使用它。基本上传递一个数组,其中包含您想要的片段的键并使用它们。
abstract class Shape {
abstract protected function getArea($data = null); //Default to null incase it is not passed.
}
class Square extends Shape {
public function getArea($data) //$data should have a length key
{
if(isset($data)){
return pow($data['length'], 2);
}
}
}
class Triangle extends Shape {
public function getArea($data) //$data should have a base and height key
{
if(isset($data)){
return .5 * $data['base'] * $data['height'];
}
}
}
正如你提到的问题下的评论,有几种方法可以解决你的问题。
Class 属性和构造函数
在我看来,这将是最简单的方法。简单又智能。
interface Shape
{
protected function getShape();
}
class Square implements Shape
{
protected $length;
public function __construct(int $length)
{
$this->length = $length;
}
protected function shape()
{
return pow($this->length, 2);
}
}
class Triangle implements Shape
{
protected $base;
protected $height;
public function __construct(int $base, int $height)
{
$this->base = $base;
$this->height = $height;
}
protected function getShape()
{
return .5 * $this->base * $this->height;
}
}
每个 class 实现 Shape 接口。 getShape 方法没有属性。这些属性是 class 本身的受保护属性。您在调用特定 class 的构造函数时设置这些属性。
我在 PHP 中遇到了一个 OOP 问题。我试图实现一个抽象的 parent class 方法,并且从 child class 开始,我必须将它与可变数量的参数一起使用。
这是抛出的错误:
PHP Fatal error: Declaration of Square::getArea($length) must be compatible with Shape::getArea()
和 classes :
abstract class Shape {
abstract protected function getArea();
}
class Square extends Shape {
public function getArea($length)
{
return pow($length, 2);
}
}
class Triangle extends Shape {
public function getArea($base, $height)
{
return .5 * $base * $height;
}
}
我可以使用 child 的 __construct()
方法在启动时设置不同形状的属性,但我想知道是否存在其他方法并允许我定义可变参数列表。
提前致谢。
我认为使用 __construct
的想法确实是最好的方法。这就是它的用途。您希望每个形状都不同,并且每个形状都必须以不同方式计算面积。因此多态性和 OOP 设计原则。
话虽如此,但总有其他黑客攻击。我不推荐这种方法,但您可以根据需要使用它。基本上传递一个数组,其中包含您想要的片段的键并使用它们。
abstract class Shape {
abstract protected function getArea($data = null); //Default to null incase it is not passed.
}
class Square extends Shape {
public function getArea($data) //$data should have a length key
{
if(isset($data)){
return pow($data['length'], 2);
}
}
}
class Triangle extends Shape {
public function getArea($data) //$data should have a base and height key
{
if(isset($data)){
return .5 * $data['base'] * $data['height'];
}
}
}
正如你提到的问题下的评论,有几种方法可以解决你的问题。
Class 属性和构造函数 在我看来,这将是最简单的方法。简单又智能。
interface Shape
{
protected function getShape();
}
class Square implements Shape
{
protected $length;
public function __construct(int $length)
{
$this->length = $length;
}
protected function shape()
{
return pow($this->length, 2);
}
}
class Triangle implements Shape
{
protected $base;
protected $height;
public function __construct(int $base, int $height)
{
$this->base = $base;
$this->height = $height;
}
protected function getShape()
{
return .5 * $this->base * $this->height;
}
}
每个 class 实现 Shape 接口。 getShape 方法没有属性。这些属性是 class 本身的受保护属性。您在调用特定 class 的构造函数时设置这些属性。