PHP构造函数

时间:2013-03-31 03:49:49

标签: php

问题:比较这两段代码,为什么第一段没有打印出来?
当我使用eclipse运行这些代码时,什么都没发生,我不知道它为什么不打印“构造正在运行”。

然而,当我运行第二段代码时,打印出来:
“建筑正在运行......” “管理器构造正在运行......”
为什么吗

第一段:

<?php
class Employee 
{
    protected $name;
    protected $title;

    function _construct()
    {
      echo "The constrcut is running..." . '<br>'';
    }   
}
$employee = new Employee();
?>

第二段

<?php
class Employee 
{
    protected $name;
    protected $title;

    function _construct()
    {
      echo "The constrcut is running..." . '<br>'';
    }   
}

class Manager extends Employee
{

    function __construct()
    {
      parent:: _construct();
      echo  "The Manager construct is running...";
    }
}
 $employee = new Manager();
?>

1 个答案:

答案 0 :(得分:0)

在课程Employee中替换:

function _construct()
{
  echo "The constrcut is running..." . '<br>'';
}  

使用:

function __construct()
{
  echo "The constrcut is running..." . '<br>'';
}  

请注意双下划线(__)。


此处您还有语法错误:

echo "The constrcut is running..." . '<br>'';
-------------------------------------------^ Why two 's?