Why aren't parent constructors called?

I added parent::__construct();tables and bookmarks to the constructors to make this code work. Why aren't they called automatically?

If I create an object of type bookmark $obj_ref_bo = new bookmark();, I should not add bookmarks and create objects from each of my parent classes (besides abstract classes).

Call chain

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')");
    }
  }
+3
source share
2 answers

do not add bookmarks and create objects from each parent class

To completely select your choice, there is no requirement in the language to call parent methods.

As the PHP manual says briefly:

. , . , parent::__construct() .

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

+7

Java no-arg , , PHP . , , , - , .;)

, , , self::, $this->. self::.

+1

All Articles