PHP的继承,动态特性和新的静态()构造函数 [英] Php Inheritance, dynamic properties and new static() constructor

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

问题描述

我来自一个.NET的世界。现在进入这些寒冷的PHP水域。

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

我发现,让我有点糊涂了一个例子。当然,我想OOP基本面适用于这个PHP code,但它没有意义。

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 app\models;

class User extends \yii\base\Object implements \yii\web\IdentityInterface
{
    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)的所有它做的是创造用户的静态新实例。这是抓住了我猝不及防的第一件事。

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决定了其具有创建一个用户对象?反射?这使我的下一个问题....看着它继承的类的对象时,在构造,有一个在一个参数是一个阵列(上面的阵列的元件之一)。

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 ::配置(这$,$配置),并在此方法中,我看到它的样子,Yii中也加入到$这个(对象实例我假设,不用户一个)属于用户的参数。

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的角度来看, $这种对象引用对象实例本身,而不是用户实例,从它继承(像我朋友说的)。我告诉他这是违反基本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.

<一个href="http://forums.phpfreaks.com/topic/286702-php-inheritance-dynamic-properties-and-new-static-constructor/" rel="nofollow">http://forums.phpfreaks.com/topic/286702-php-inheritance-dynamic-properties-and-new-static-constructor/

享受!

这篇关于PHP的继承,动态特性和新的静态()构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆