<bdo id='gVUYf'></bdo><ul id='gVUYf'></ul>

    1. <small id='gVUYf'></small><noframes id='gVUYf'>

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

    3. <tfoot id='gVUYf'></tfoot>

        类属性中的 PHP 函数调用

        时间:2023-10-05
        <i id='Ltse9'><tr id='Ltse9'><dt id='Ltse9'><q id='Ltse9'><span id='Ltse9'><b id='Ltse9'><form id='Ltse9'><ins id='Ltse9'></ins><ul id='Ltse9'></ul><sub id='Ltse9'></sub></form><legend id='Ltse9'></legend><bdo id='Ltse9'><pre id='Ltse9'><center id='Ltse9'></center></pre></bdo></b><th id='Ltse9'></th></span></q></dt></tr></i><div id='Ltse9'><tfoot id='Ltse9'></tfoot><dl id='Ltse9'><fieldset id='Ltse9'></fieldset></dl></div>

          <tfoot id='Ltse9'></tfoot>
            • <bdo id='Ltse9'></bdo><ul id='Ltse9'></ul>

                    <tbody id='Ltse9'></tbody>

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

                1. <legend id='Ltse9'><style id='Ltse9'><dir id='Ltse9'><q id='Ltse9'></q></dir></style></legend>

                  本文介绍了类属性中的 PHP 函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为什么我不能在 PHP 中执行此操作?其中 Database 是一个单例类,getInstance() 返回一个 PDO 对象.

                  query("SELECT * FROM blah");返回 $stmt->fetch();}}

                  <块引用>

                  与任何其他 PHP 静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式.因此,虽然您可以将静态属性初始化为整数或数组(例如),但您不能将其初始化为另一个变量、函数返回值或对象.

                  http://php.net/manual/en/language.oop5.static.php

                  为什么?!

                  解决方案

                  http://php.net/language.oop5.properties

                  <块引用>

                  类成员变量称为属性".您可能还会看到使用其他术语(例如属性"或字段")来引用它们,但出于此引用的目的,我们将使用属性".它们是通过使用关键字 public、protected 或 private 之一来定义的,后跟一个普通的变量声明.此声明可能包含一个初始化,但此初始化必须是一个常量值——也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估.强>

                  最重要的是

                  <块引用>

                  也就是说,它必须能够在编译时被评估

                  表达式是在运行时求值的,所以不能使用表达式来初始化属性:它们根本就不能求值.

                  Why can't I do this in PHP? Where Database is a singleton class and getInstance() returns a PDO object.

                  <?php
                  
                  class AnExample
                  {
                      protected static $db = Database::getInstance();
                  
                      public static function doSomeQuery()
                      {
                          $stmt = static::$db->query("SELECT * FROM blah");
                          return $stmt->fetch();
                      }
                  }
                  

                  Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

                  http://php.net/manual/en/language.oop5.static.php

                  Why?!

                  解决方案

                  http://php.net/language.oop5.properties

                  Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

                  The important part is

                  that is, it must be able to be evaluated at compile time

                  Expressions were evaluated at runtime, so it isn't possible to use expression to initialize properties: They are simply not evaluatable yet.

                  这篇关于类属性中的 PHP 函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDOException 没有被捕获? 下一篇:如何使用ajax将输入文件数据值发送到php页面

                  相关文章

                  最新文章

                  • <bdo id='pZubh'></bdo><ul id='pZubh'></ul>
                2. <i id='pZubh'><tr id='pZubh'><dt id='pZubh'><q id='pZubh'><span id='pZubh'><b id='pZubh'><form id='pZubh'><ins id='pZubh'></ins><ul id='pZubh'></ul><sub id='pZubh'></sub></form><legend id='pZubh'></legend><bdo id='pZubh'><pre id='pZubh'><center id='pZubh'></center></pre></bdo></b><th id='pZubh'></th></span></q></dt></tr></i><div id='pZubh'><tfoot id='pZubh'></tfoot><dl id='pZubh'><fieldset id='pZubh'></fieldset></dl></div>
                3. <small id='pZubh'></small><noframes id='pZubh'>

                4. <tfoot id='pZubh'></tfoot>

                  <legend id='pZubh'><style id='pZubh'><dir id='pZubh'><q id='pZubh'></q></dir></style></legend>