2015-01-21 63 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' ];