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

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

    1. <tfoot id='fmMdf'></tfoot>

      1. Php 继承、动态属性和新的 static() 构造函数

        Php Inheritance, dynamic properties and new static() constructor(Php 继承、动态属性和新的 static() 构造函数)
      2. <legend id='yJ42c'><style id='yJ42c'><dir id='yJ42c'><q id='yJ42c'></q></dir></style></legend>
          <tbody id='yJ42c'></tbody>

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

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

                  本文介绍了Php 继承、动态属性和新的 static() 构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我来自 .NET 世界.现在进入这些寒冷的 php 水域.

                  I come from a .NET world. Now entering these frigid php waters.

                  我发现了一个让我有点困惑的例子.当然,我正在尝试将 OOP 基础应用于这段 php 代码,但这没有意义.

                  I found an example that got me a little confused. Of course, I am trying to apply OOP fundamentals to this php code but it doesn't make sense.

                  这就是我正在谈论的课程.

                  This is the class i am talking about.

                  <?php
                  
                  namespace appmodels;
                  
                  class User extends yiiaseObject implements yiiwebIdentityInterface
                  {
                      public $id;
                      public $username;
                      public $password;
                      public $authKey;
                  
                      private static $users = [
                          '100' => [
                              'id' => '100',
                              'username' => 'admin',
                              'password' => 'admin',
                              'authKey' => 'test100key',
                          ],
                          '101' => [
                              'id' => '101',
                              'username' => 'demo',
                              'password' => 'demo',
                              'authKey' => 'test101key',
                          ],
                      ];
                  
                      public static function findIdentity($id)
                      {
                          return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
                      }
                  
                      public static function findByUsername($username)
                      {
                          foreach (self::$users as $user) {
                              if (strcasecmp($user['username'], $username) === 0) {
                                  return new static($user);
                              }
                          }
                          return null;
                      }
                  
                      public function getId()
                      {
                          return $this->id;
                      }
                  
                      public function getAuthKey()
                      {
                          return $this->authKey;
                      }
                  
                      public function validateAuthKey($authKey)
                      {
                          return $this->authKey === $authKey;
                      }
                  
                      public function validatePassword($password)
                      {
                          return $this->password === $password;
                      }
                  }
                  

                  好的,对我来说很明显,在 findByIdentity($id) 方法中,它所做的只是创建一个静态的 User 新实例.这是让我措手不及的第一件事.

                  Alright, it's obvious to me that in the method findByIdentity($id) all it's doing is creating a static new instance of User. This is the first thing that caught me off guard.

                  在 .net 中,您不能创建静态类的实例.

                  In .net you cannot create an instance of a static class.

                  现在,继续.在那一行

                  return isset(self::$users[$id])? new static(self::$users[$id]) : null;
                  

                  让我感兴趣的第二件事是以下内容.

                  the second thing that intrigues me is the following.

                  因为该数组中只有一个键/值集合....

                  Since all you have in that array is a key/value collection....

                  private static $users = [
                          '100' => [
                              'id' => '100',
                              'username' => 'admin',
                              'password' => 'admin',
                              'authKey' => 'test100key',
                          ],
                          '101' => [
                              'id' => '101',
                              'username' => 'demo',
                              'password' => 'demo',
                              'authKey' => 'test101key',
                          ],
                      ];
                  

                  php 是如何确定它必须创建一个 User 对象的?反射?这让我想到了下一个问题......查看它继承的类,对象,在构造函数中,有一个参数是一个数组(上面数组的元素之一).

                  how does php determine that it has to create an User object? Reflection? Which leads me to the next question.... looking at the class that it inherits from, Object, in the constructor, there's in one parameter which is an array (one of the elements of the array above).

                  public function __construct($config = [])
                  {
                      if (!empty($config)) {
                          Yii::configure($this, $config);
                      }
                      $this->init();
                  }
                  

                  但是,这个类在它的构造函数中,正在调用 Yii::configure($this, $config) 并且在这个方法中,在我看来,Yii 添加到 $this (我假设的对象实例,而不是用户一)属于用户的参数.

                  BUT, this class in its constructor, is calling Yii::configure($this, $config) and in this method, the way I see it, Yii is adding to $this (the Object instance I am assuming, not the User one) the parameters that belong to User.

                  public static function configure($object, $properties)
                  {
                      foreach ($properties as $name => $value) {
                          $object->$name = $value;
                      }
                      return $object;
                  }
                  

                  在我看来,它正在向对象动态添加参数,用户将通过匹配的参数访问这些参数.

                  Seems to me like it's adding parameters dynamically to Object which will be accessed by User via the matching parameters.

                  有意义吗?

                  从我的 .net 角度来看,Object 中的 $this 指的是 Object 实例本身,而不是从它继承的 User 实例(就像我朋友说的).我告诉他这违反了基本的 OOP 原则,这是不可能的.

                  From my .net standpoint, $this in Object refers to Object instance itself, not to the User instance inheriting from it (like my friend says). I told him that's a violation of basic OOP principles and it's simply impossible.

                  有谁能帮我理解一下吗?

                  Anyone that could make me understand about this?

                  谢谢.

                  推荐答案

                  对于任何有兴趣的人,这里有一个很好的答案和冗长的解释.

                  For anyone interested, here is a good answer with a lengthy explanation.

                  http://forums.phpfreaks.com/topic/286702-php-inheritance-dynamic-properties-and-new-static-constructor/

                  享受吧!

                  这篇关于Php 继承、动态属性和新的 static() 构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    • <small id='u7EEq'></small><noframes id='u7EEq'>

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