php中抽象类的二个例子,一个简单,一个复杂点,是学习php抽象类的不错的例子,有需要的朋友参考下。
php抽象类
<?php
//定义一个抽象类
abstract class staff
{
abstract function hire();
abstract function fire();
abstract function promote();
abstract function demote();
}
?>
php抽象类的例子
<?php
class employee {
private $title;
private $lastname;
private $firstname;
protected $salary;
private $ratio = 0;
public function construct($title, $firstname, $mainname, $salary ) {
$this->title = $title;
$this->firstname = $firstname;
$this->lastname = $mainname;
$this->salary = $salary;
}
public function firstname() {
return $this->firstname;
}
public function getlastname() {
return $this->lastname;
}
public function setratio( $num ) {
$this->ratio=$num;
}
public function getratio() {
return $this->ratio;
}
public function gettitle() {
return $this->title;
}
public function getsalary() {
return ($this->salary - $this->ratio);
}
public function getfullname() {
return "{$this->firstname}" . " {$this->lastname}";
}
function getsummaryline() {
$base = "$this->title ( $this->lastname, ";
$base .= "$this->firstname )";
return $base;
}
}
//定义抽象类
abstract class employeewriter {
abstract static function write( employee $shopproduct );
}
class textemployeewriter extends employeewriter {
static function write( employee $shopemployee ) {
$str = "{$shopemployee->gettitle()}: ";
$str .= $shopemployee->getfullname();
$str .= " ({$shopemployee->getsalary()})\n";
print $str;
}
}
$developer1 = new employee("a", "a1", "a2", 5.99 );
textemployeewriter::write( $developer1 );
?>
以上就是php抽象类的实现方法详解的详细内容。