PHP数组重构到对象

时间:2015-01-21 12:03:13

标签: php arrays refactoring getter-setter

我在传统代码中有一个真正的GIANT数组。像db条目中的500k +。一旦用户登录就会获得populatet。可以这么说全局用户数组。

现在,我完成了对这个男孩进行重构的不必要的追求。

数组是一维的数组,如

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

现在我想在一个对象中重构它,只有当我真正需要它时才会调用DB中的值。我的想法是用一个对象替换数组assighnment部分。

所以$user = array();我需要用户$user = new user();

是否有任何已知的方法来访问类函数,以便我可以通过$user['name']访问其属性,以便将其传递给__get method

我知道这是一项艰巨的任务,而且可能是不可能的。但无论如何,我会问:)

3 个答案:

答案 0 :(得分: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'];

答案 1 :(得分: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关联数组一样进行获取和设置:

<?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' ];

答案 2 :(得分:0)

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