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

2022-01-04 00:00:00 php c# yii .net

我来自 .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/

享受吧!

相关文章