Why aren't parent constructors being called?

不羁岁月 提交于 2019-12-31 01:48:23

问题


I added in parent::__construct(); to the constructors of table and bookmark in order to get this code to work. Why are they not called automatically?

If I create an object of type bookmark $obj_ref_bo = new bookmark(); should not bookmark also create objects from each of its parent classes (besides abstract classes).

The call chain is

bookmark->table->database(abstract)->single_connect

    /*single_connect*/

class single_connect
  {
  protected static $_db_pointer = NULL;
  private function __construct()
    {
    $this->_db_pointer = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_DATABASE);
    }
  public static function get_connection()
    {
    if(self::$_db_pointer == NULL)
      {
      return new self();
      } 
    else
      {
      echo "Error:only one connection";
      }
    }
  }

/*database*/

abstract class database
  {
  protected function __construct()
    {
    single_connect::get_connection();
    }
  static protected function query($query)
    {
    $result = mysql_query($query) or die(mysql_error());
    return $result;
    }
  }

/*table*/

class table extends database
  {
  public $_protected_arr=array();
  protected function __construct()
    {
    parent::__construct();
    $this->protect();
    }
  protected function protect()
    {
    foreach($_POST as $key => $value)
      {
      $this->_protected_arr[$key] = mysql_real_escape_string($value);
      }
    }
  }

/*bookmark*/

class bookmark extends table 
  {
  function __construct()
    {
    parent::__construct();
    $this->start();
    }   
  function start()
    {
    if(this->test())
      {
      this->insert();
      }
    else
      {
      return 1;
      }
    }
  function test()
    {
    if(this->test_empty())
      {
      return 1;
      }
    else
      {
      return 0;
      }
    }
  function test_empty()
    {
    if(text::test_empty($this->_protected_arr)) 
      {
      return 1;
      }
    else
      {
      return 0;
      }
    }
  function insert()
    {
    $url =  $this->_protected_arr['url'];
    $title =  $this->_protected_arr['title'];
    $email = $_SESSION['email'];
    database::query("INSERT INTO bo VALUES ('$title', '$url', '$email')");
    }
  }

回答1:


should not bookmark also create objects from each of its parent classes

That's entirely your choice to make, there is no requirement in the language to call the parent methods.

As the PHP manual concisely puts it:

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

— http://php.net/oop5.decon




回答2:


While Java will call the no-arg constructor of the parent class if you don't call one specifically, PHP has no such feature. This allows you to do some work before calling the parent constructor, though you obviously shouldn't depend on any properties set by the parent constructor. ;)

BTW, as I stated in a comment, most of your calls that use self:: should be using $this-> instead. Only static methods are called using self::.



来源:https://stackoverflow.com/questions/6220848/why-arent-parent-constructors-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!