网络编程 
首页 > 网络编程 > 浏览文章

PHP面向对象程序设计继承用法简单示例

(编辑:jimmy 日期: 2024/5/31 浏览:3 次 )

本文实例讲述了PHP面向对象程序设计继承用法。分享给大家供大家参考,具体如下:

<"<p>$this->name : ",self::$count," : parent : __construct</p>";
  }
  function work(){
    echo "<p>$this->name is working</p>";
  }
  
  function __destruct(){
    echo "<p>parent unset $this->name</p>";
  }
}
class Managers extends Employees{
  private $pos = null;
  function __construct($p,$nameStr){
    parent::$count++;
    parent::__construct($nameStr);
    $this->pos = $p;
    echo "<p>$this->name , $this->pos : self : __construct</p>";
  }
  function assignJob(){
    echo "<p>$this->name assign jobs</p>";
  }
  function getName(){
    return $this->name;
  }
  function __destruct(){
    echo "<p>self unset $this->name</p>";
  }
}
class Programmers extends Employees{
  function code(){
    echo "<p>$this->name is coding</p>";
  }
  function getName(){
    return $this->name;
  }
}
$e1 = new Employees('e1');
$e2 = new MAnagers(2,'e2');
$e3 = new Programmers('e3');
$e1->work();
$e2->work();
$e3->work();
$e2->assignJob();
$e3->Code();
echo "<p>{$e3->getName()}</p>";
//echo "<p>$e1->name</p>";
if($e2 instanceof Employees){
  echo "<p>ok</p>";
}else{
  echo "<p>no</p>";
}
unset($e1,$e2,$e3);

运行结果:

e1 : 0 : parent : __construct

e2 : 1 : parent : __construct

e2 , 2 : self : __construct

e3 : 1 : parent : __construct

e1 is working

e2 is working

e3 is working

e2 assign jobs

e3 is coding

e3

ok

parent unset e1

self unset e2

parent unset e3

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

上一篇:PHP实现函数内修改外部变量值的方法示例
下一篇:PHP抽象类基本用法示例
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网