PHP数组重构到对象

PHP数组重构到对象,php,arrays,refactoring,getter-setter,Php,Arrays,Refactoring,Getter Setter,我在遗留代码中有一个非常巨大的数组。例如,来自db入口的500k+电压。它在用户登录时会被populatet一次。可以说是全局用户数组 现在我得到了重塑这个坏男孩的不遗余力的追求 该数组是一个一维辅助数组,类似 $data['username'] = 'idiots'; ( and tons of other values) 现在我想在一个对象中重构它,这个对象只在我真正需要的时候从DB调用值。我的想法是用一个对象替换数组辅助部分 所以这里$user=array()我想要用户$user=ne

我在遗留代码中有一个非常巨大的数组。例如,来自db入口的500k+电压。它在用户登录时会被populatet一次。可以说是全局用户数组

现在我得到了重塑这个坏男孩的不遗余力的追求

该数组是一个一维辅助数组,类似

 $data['username'] = 'idiots'; ( and tons of other values)
现在我想在一个对象中重构它,这个对象只在我真正需要的时候从DB调用值。我的想法是用一个对象替换数组辅助部分

所以这里
$user=array()我想要用户
$user=new user()

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


我知道这是一项艰巨的任务,而且可能是不可能的。但是我还是要问:)

一种方法是创建一个类来实现并将延迟加载逻辑放入
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'];

一种方法是创建一个类来实现延迟加载逻辑,并将其放入
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'];

一种方法是创建一个类来实现延迟加载逻辑,并将其放入
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'];

一种方法是创建一个类来实现延迟加载逻辑,并将其放入
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'];

有两种选择。第一个是传统的:

<?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

/**
 * @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

/**
 * @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

/**
 * @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;

错误方法都德:)我通过$data['username']在超过百万的Acessor中输入了遗留代码,这是我的问题。这要求您仍然首先检索500k值。错误方法都德:)我通过$data['username']在超过百万的Acessor中输入了遗留代码这是我的问题。这要求你仍然首先检索500k值。错误的方法都德:)我通过$data['username']在超过百万的Acessor中有遗留代码,这是我的问题。这要求你仍然首先检索500k值。错误的方法都德:)我通过$data['username']在超过百万的Acessor中有遗留代码这就是我的问题。这要求您仍然首先检索500k值。