为什么没有调用父构造函数?

发布于 2024-11-13 06:12:04 字数 2147 浏览 1 评论 0原文

我将 parent::__construct(); 添加到表和书签的构造函数中,以使此代码正常工作。为什么它们不被自动调用?

如果我创建一个书签类型的对象 $obj_ref_bo = new bookmark(); 不应该为书签也从它的每个父类(除了抽象类)创建对象。

调用链为

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')");
    }
  }

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')");
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-11-20 06:12:04

不应该为每个父类创建对象添加书签

这完全是您的选择,语言中没有调用父方法的要求

正如 PHP 手册简洁地指出的那样:

注意:如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用 parent::__construct()

http://php.net/oop5.decon

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

秋日私语 2024-11-20 06:12:04

如果您不专门调用父类的无参数构造函数,Java 会调用父类的无参数构造函数,而 PHP 则没有这样的功能。这允许您在调用父构造函数之前做一些工作,尽管您显然不应该依赖父构造函数设置的任何属性。 ;)

顺便说一句,正如我在评论中所说,大多数使用 self:: 的调用应该使用 $this-> 代替。仅使用 self:: 调用静态方法。

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::.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文