【发布时间】:2014-11-08 12:07:38
【问题描述】:
我是面向对象的 php 新手。如果 HumanClass 的方法 testing() 中没有函数,我应该将它们声明为抽象吗?
<?php
class HumanClass
{
private $legs;
private $hands;
public function __construct($legs, $hands)
{
$this->legs = $legs;
$this->hands = $hands;
}
public function testing()
{
}
}
class StudentClass extends HumanClass
{
private $books;
public function __construct($legs, $hands, $books)
{
parent::__construct($legs, $hands);
$this->books = $books;
}
public function testing()
{
echo "StudentClass called.";
}
}
function callClass(HumanClass $c)
{
$c->testing();
}
$example = new StudentClass(4, 2, 1);
callClass($a);
?>
有可能有这样的东西吗?
echo $a->testing();
而不是有另一个方法来调用 testing()。
【问题讨论】:
-
这是错字吗?
$this->legs = legs;没有$和其他一些 -
@John:是什么阻止了您first检查 PHP 文档?
标签: php oop polymorphism abstract-class