2
我一直在使用5.6,但它有动态类型的真正限制。我刚刚查看了PHP7的文档,最终看起来他们正在削减困扰旧版本的问题,看起来他们实际上是设计现在的语言。PHP7是否支持多态?
我看到它支持参数类型提示,这是否意味着我们实际上可以具有多态函数?
还有一个问题,切线相关但是PHP7的当前版本是一个稳定版本?
我一直在使用5.6,但它有动态类型的真正限制。我刚刚查看了PHP7的文档,最终看起来他们正在削减困扰旧版本的问题,看起来他们实际上是设计现在的语言。PHP7是否支持多态?
我看到它支持参数类型提示,这是否意味着我们实际上可以具有多态函数?
还有一个问题,切线相关但是PHP7的当前版本是一个稳定版本?
关于你对函数参数的类型提示的问题,答案是“是”,PHP在这方面支持多态。
我们可以采用矩形和三角形的典型形状示例。让我们先定义这三个类别:
Shape类
class Shape {
public function getName()
{
return "Shape";
}
public function getArea()
{
// To be overridden
}
}
Rectangle类
class Rectangle extends Shape {
private $width;
private $length;
public function __construct(float $width, float $length)
{
$this->width = $width;
$this->length = $length;
}
public function getName()
{
return "Rectangle";
}
public function getArea()
{
return $this->width * $this->length;
}
}
三角类
class Triangle extends Shape {
private $base;
private $height;
public function __construct(float $base, float $height)
{
$this->base = $base;
$this->height = $height;
}
public function getName()
{
return "Triangle";
}
public function getArea()
{
return $this->base * $this->height * 0.5;
}
}
现在我们可以编写一个采用上述Shape类的函数。
function printArea(Shape $shape)
{
echo "The area of `{$shape->getName()}` is {$shape->getArea()}" . PHP_EOL;
}
$shapes = [];
$shapes[] = new Rectangle(10.0, 10.0);
$shapes[] = new Triangle(10.0, 10.0);
foreach ($shapes as $shape) {
printArea($shape);
}
一个例子运行会产生以下结果:
The area of `Rectangle` is 100
The area of `Triangle` is 50
关于你提到的有关PHP7稳定的第二个问题:是的,PHP7稳定,许多公司在生产中使用。
PHP不支持Java中的多态(我怀疑这是你所问的) - 请参阅[本文](http://code.tutsplus.com/tutorials/understanding-and-applying-多态性 - 在PHP中 - 网络-14362)或[这一个](http://phpenthusiast.com/object-oriented-php-tutorials/polymorphism-in-php),它适用于PHP7尽可能多PHP5 –
自11月以来,PHP7一直保持稳定版本 –