1. <i id='jj3QK'><tr id='jj3QK'><dt id='jj3QK'><q id='jj3QK'><span id='jj3QK'><b id='jj3QK'><form id='jj3QK'><ins id='jj3QK'></ins><ul id='jj3QK'></ul><sub id='jj3QK'></sub></form><legend id='jj3QK'></legend><bdo id='jj3QK'><pre id='jj3QK'><center id='jj3QK'></center></pre></bdo></b><th id='jj3QK'></th></span></q></dt></tr></i><div id='jj3QK'><tfoot id='jj3QK'></tfoot><dl id='jj3QK'><fieldset id='jj3QK'></fieldset></dl></div>
    <tfoot id='jj3QK'></tfoot>
  2. <legend id='jj3QK'><style id='jj3QK'><dir id='jj3QK'><q id='jj3QK'></q></dir></style></legend>

    <small id='jj3QK'></small><noframes id='jj3QK'>

      • <bdo id='jj3QK'></bdo><ul id='jj3QK'></ul>

      __construct 函数是做什么用的?

      What is the function __construct used for?(__construct 函数是做什么用的?)

      <tfoot id='hYW3j'></tfoot>
      • <bdo id='hYW3j'></bdo><ul id='hYW3j'></ul>
          <legend id='hYW3j'><style id='hYW3j'><dir id='hYW3j'><q id='hYW3j'></q></dir></style></legend>

          <small id='hYW3j'></small><noframes id='hYW3j'>

              <tbody id='hYW3j'></tbody>

            <i id='hYW3j'><tr id='hYW3j'><dt id='hYW3j'><q id='hYW3j'><span id='hYW3j'><b id='hYW3j'><form id='hYW3j'><ins id='hYW3j'></ins><ul id='hYW3j'></ul><sub id='hYW3j'></sub></form><legend id='hYW3j'></legend><bdo id='hYW3j'><pre id='hYW3j'><center id='hYW3j'></center></pre></bdo></b><th id='hYW3j'></th></span></q></dt></tr></i><div id='hYW3j'><tfoot id='hYW3j'></tfoot><dl id='hYW3j'><fieldset id='hYW3j'></fieldset></dl></div>
              • 本文介绍了__construct 函数是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我已经注意到 __construct 很多关于类.我做了一些阅读和网上冲浪,但我找不到我能理解的解释.我刚开始接触 OOP.

                I have been noticing __construct a lot with classes. I did a little reading and surfing the web, but I couldn't find an explanation I could understand. I am just beginning with OOP.

                我想知道是否有人可以让我大致了解它是什么,然后是一个如何与 PHP 一起使用的简单示例?

                I was wondering if someone could give me a general idea of what it is, and then a simple example of how it is used with PHP?

                推荐答案

                __construct 是在 PHP5 中引入的,它是定义构造函数的正确方法(在 PHP4 中你使用了构造函数的类).您不需要在类中定义构造函数,但是如果您希望在对象构造中传递任何参数,那么您需要一个.

                __construct was introduced in PHP5 and it is the right way to define your, well, constructors (in PHP4 you used the name of the class for a constructor). You are not required to define a constructor in your class, but if you wish to pass any parameters on object construction then you need one.

                一个例子可能是这样的:

                An example could go like this:

                class Database {
                  protected $userName;
                  protected $password;
                  protected $dbName;
                
                  public function __construct ( $UserName, $Password, $DbName ) {
                    $this->userName = $UserName;
                    $this->password = $Password;
                    $this->dbName = $DbName;
                  }
                }
                
                // and you would use this as:
                $db = new Database ( 'user_name', 'password', 'database_name' );
                

                PHP 手册中解释了其他所有内容:点击此处

                Everything else is explained in the PHP manual: click here

                这篇关于__construct 函数是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I pass parameters into a PHP script through a webpage?(如何通过网页将参数传递给 PHP 脚本?)
                PHP - include a php file and also send query parameters(PHP - 包含一个 php 文件并发送查询参数)
                Where can I read about conditionals done with quot;?quot; and quot;:quot; (colon)?(我在哪里可以阅读有关使用“?完成的条件的信息?和“:(冒号)?)
                Accessing arrays whitout quoting the key(在不引用键的情况下访问数组)
                What is the name for the quot;lt;lt;lt;quot; operator?(“lt;lt;lt;的名字是什么?操作员?)
                default as first option in switch statement?(默认为 switch 语句中的第一个选项?)
                <tfoot id='8IOQN'></tfoot>

                • <legend id='8IOQN'><style id='8IOQN'><dir id='8IOQN'><q id='8IOQN'></q></dir></style></legend>
                    <tbody id='8IOQN'></tbody>

                        • <bdo id='8IOQN'></bdo><ul id='8IOQN'></ul>
                          <i id='8IOQN'><tr id='8IOQN'><dt id='8IOQN'><q id='8IOQN'><span id='8IOQN'><b id='8IOQN'><form id='8IOQN'><ins id='8IOQN'></ins><ul id='8IOQN'></ul><sub id='8IOQN'></sub></form><legend id='8IOQN'></legend><bdo id='8IOQN'><pre id='8IOQN'><center id='8IOQN'></center></pre></bdo></b><th id='8IOQN'></th></span></q></dt></tr></i><div id='8IOQN'><tfoot id='8IOQN'></tfoot><dl id='8IOQN'><fieldset id='8IOQN'></fieldset></dl></div>

                          <small id='8IOQN'></small><noframes id='8IOQN'>