2014-02-28 142 views
1

我來自.NET世界。現在進入這些寒冷的PHP水域。Php繼承,動態屬性和新的靜態()構造函數

我發現一個讓我有點困惑的例子。當然,我正在嘗試將OOP基礎應用於此PHP代碼,但它沒有任何意義。

這是我正在談論的課程。

<?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)的所有它做的是創造用戶的靜態新實例。這是第一件讓我措手不及的事情。

在.net中,您無法創建靜態類的實例。

現在,繼續。在那一行

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

第二件讓我感興趣的事情是以下幾點。

既然你數組中有一個鍵/值集合....

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

如何PHP決定了其具有創建一個用戶對象?反射?這導致我到下一個問題....看看它繼承的類,Object,在構造函數中,有一個參數是一個數組(上面數組的一個元素)。

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

,但這個類在其構造,呼籲的Yii ::配置($此,$配置),並在此方法中,我看到它的方式,Yii中也加入到了這個$(對象實例我我假設,而不是用戶之一)屬於用戶的參數。

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

對我來說似乎是動態地將參數添加到Object中,這將通過匹配參數由User訪問。

有意義嗎?

從我的角度看.NET,$此對象是指對象實例本身,而不是給用戶實例從它繼承(像我朋友說的)。我告訴他這違反了基本的面向對象原則,這根本不可能。

任何能夠讓我明白這一點的人?

謝謝。

回答

1

在PHP中沒有這樣的靜態類,你可以有靜態方法和靜態成員。但是每個類都可以被實例化。你可以在構造函數中拋出一個Exception。

Now ... on the static keyword。在你使用它的上下文中,它調用了後期靜態綁定類的構造函數。這裏有一些代碼可以幫助你。

class Something { 
    public static function getInstance() { 
     return new static(); 
    } 
} 

class Other extends Something { 

} 

當你從Other上下文調用getInstance,什麼getInstance所做的就是調用構造函數類Other

// this will print `Other` 
echo get_class(Other::getInstance()); 

而且,因爲你從yii\base\Object類繼承,和你」沒有在User中定義構造函數,被調用的構造函數是yii\base\Object中的構造函數


現在你的問題

從我的角度來看.NET的第二部分,$這對象是指對象實例本身,而不是給用戶實例從它繼承(像我朋友說的)。我告訴他這違反了基本的面向對象原則,這根本不可能。

$無論對象是從哪個類實例化的,都會返回實際的對象。你也可以在.NET中做到這一點,儘管你需要投射你的物體。但是爲了沒有強大的打字功能的PHP,對象按照原樣呈現,您可以按原樣使用它。

+0

謝謝你的時間。這不是我的代碼。我試圖弄清楚PHP的意義。也許我沒有正確解釋自己,但你的答案似乎適用於我的問題。 –