2015-01-21 61 views
0

我有一個真正的GIANT陣列在傳統代碼。比如來自db條目的500k +。它在用戶登錄時得到一次populatet。可以這麼說全球用戶陣列。PHP陣列重構到對象

現在,我得到了重建這個孩子的未知任務。

數組就像

$data['username'] = 'idiots'; (and tons of other values) 

一個一維數組assighned現在我想refacture這一個對象至極會從數據庫中調用值只有當我真的需要它。我的想法是用一個對象替換數組assahnment部分。

那麼$user = array();我想用戶$user = new user();

是否有任何已知的方式來訪問類函數,所以我可以通過$user['name']來訪問它的屬性,所以它會傳遞到__get method

我知道這是一個很高的命令,它可能是不可能的。而IL問反正:)

回答

2

一種方法是使實現ArrayAccess一類,把你的懶加載邏輯爲offsetGet

class User implements ArrayAccess { 
    private $cache = array(); 

    public function offsetSet($key, $value) { 
     throw new Exception("Read-only!"); 
    } 

    public function offsetUnset($key) { 
     throw new Exception("Read-only!"); 
    } 

    public function offsetExists($key) { 
     // consult the DB schema and return true if the `key` makes sense 
    } 

    public function offsetGet($key) { 
     if(!isset($this->cache[$key])) { 
      // load stuff from the DB 
      $this->cache[$key] = ...; 
     } 
     return $this->cache[$key]; 
    } 
} 

$u = new User(); 
print $u['name']; 
+0

先生您是一個GEANIUS!謝謝!!!!不認爲這是可能的! – Sangoku 2015-01-21 12:22:10

+0

剛剛完成執行處理程序。再次感謝你!!!! :D – Sangoku 2015-10-01 11:52:09

0
$data['username'] = 'idiots'; 
$data = (object) $data; 
echo $data->username; // prints 'idiots' 
+0

周圍老兄錯誤的方式:)我通過$ data ['用戶名']這是我的問題在超過百萬acessors遺產代碼。 – Sangoku 2015-01-21 12:21:42

+0

這要求您仍然首先檢索500k個值。 – BadHorsie 2015-01-21 12:23:08

1

有兩個備選方案這一點。第一個是傳統的:

<?php 

/** 
* @see http://php.net/manual/en/language.oop5.overloading.php#object.call 
*/ 
class User 
{ 
    protected $data = array(); 

    /** 
    * @see http://php.net/manual/en/language.oop5.overloading.php#object.get 
    */ 
    public function __get($name) 
    { 
     echo "Getting $name " . PHP_EOL; 
     return $this->data[ $name ]; 
    } 

    /** 
    * @see http://php.net/manual/en/language.oop5.overloading.php#object.set 
    */ 
    public function __set($name, $value) 
    { 
     echo "Setting $name to $value " . PHP_EOL; 
     $this->data[ $name ] = $value; 
    } 
} 

$user = new User(); 
$user->a = 'Example'; 

echo $user->a; 

第二個是使用PHP SPL接口ArrayAccess接口,允許一個對象來獲取和設置像一個PHP Assocative陣列:

<?php 

/** 
* @see http://php.net/manual/en/class.arrayaccess.php 
*/ 
class User implements ArrayAccess 
{ 
    protected $data = array(); 

    /** 
    * @see http://php.net/manual/en/arrayaccess.offsetexists.php 
    */ 
    public function offsetSet($key, $value) 
    { 
     echo "Setting $name to $value " . PHP_EOL; 
     if(empty($offset)) 
     { 
      $this->data []= $value; 
     } 
     else 
     { 
      $this->data[ $key ] = $value; 
     } 
    } 

    /** 
    * @see http://php.net/manual/en/arrayaccess.offsetget.php 
    */ 
    public function offsetExists($key) 
    { 
     return isset($this->container[ $key ]); 
    } 

    /** 
    * @see http://php.net/manual/en/arrayaccess.offsetunset.php 
    */ 
    public function offsetUnset($key) 
    { 
     unset($this->data[ $key ]); 
    } 

    /** 
    * @see http://php.net/manual/en/arrayaccess.offsetset.php 
    */ 
    public function offsetGet($offset) 
    { 
     echo "Getting $name " . PHP_EOL; 

     if($this->offsetExists($key)) 
     { 
      return $this->data[ $key ]; 
     } 

     return null; 
    } 
} 

$user = new User(); 
$user->[ 'a' ] = 'Example'; 

echo $user->[ 'a' ];