在php类中,有没有一种简单的方法可以用函数定义变量?

在php类中,有没有一种简单的方法可以用函数定义变量?,php,class,variables,Php,Class,Variables,在我的类中,我有一个这样定义的数组 class t { var $settings = array(); } 我将大量使用这些设置,因此我不想到处写$this->settings['setting'],而是想部署一个函数,在局部变量中自动定义这些设置 private function get_settings () { $array = $this->settings['array']; $foreign_key = $this-

在我的类中,我有一个这样定义的数组

class t {
    var $settings = array();
}
我将大量使用这些设置,因此我不想到处写
$this->settings['setting']
,而是想部署一个函数,在局部变量中自动定义这些设置

private function get_settings () {

            $array = $this->settings['array'];
            $foreign_key = $this->settings['foreign_key'];
            $limit = $this->settings['limit'];
            $tableclassid = $this->settings['tableclassid'];
            $pager = $this->settings['pager'];
            $container = $this->settings['container'];
            $extracolumn = $this->settings['extracolumn'];
    }
现在,我想做的是获取这些变量并在类中的另一个函数中使用它们。举例来说

public function test () {
     $this->get_settings();
     return $foreign_key;
}
我希望它返回
$this->settings['foreign\u key']

有办法做到这一点吗?或者我必须用get_settings()的代码块将所有函数弄得乱七八糟吗

我感谢你的帮助。。谢谢:)

使用内置函数,该函数将数组提取到当前范围内的单个变量中

extract($this->settings);
如果需要修改这些局部变量以反映在原始数组中,请将它们提取为引用

extract($this->settings, EXTR_REFS);
我不能说我更愿意自己使用这种方法,甚至不能建议你这样做。在类内部,将它们保存在数组属性中更容易阅读和理解。一般来说,我从未真正使用过
extract()

使用内置函数,该函数将数组提取到当前范围内的单个变量中

extract($this->settings);
如果需要修改这些局部变量以反映在原始数组中,请将它们提取为引用

extract($this->settings, EXTR_REFS);

我不能说我更愿意自己使用这种方法,甚至不能建议你这样做。在类内部,将它们保存在数组属性中更容易阅读和理解。一般来说,我从未真正使用过
extract()

只需将其作为属性传递即可。大概是这样的:

$class = new T();
然后:

$class->getSettings('varname');
在功能上:

function get_settings($varname){
    return $this->settings[$varname];
}
或者使用
\uu get()
重载功能:

function get_settings($varname){
    return $this->settings[$varname];
}
公共函数获取($name){ 返回$this->settings[$name]; }

这样称呼它:

$class->varname


(类中不存在的函数/变量,将被发送到get()重载函数,只需将其作为属性传递即可。类似如下:

$class = new T();
然后:

$class->getSettings('varname');
在功能上:

function get_settings($varname){
    return $this->settings[$varname];
}
或者使用
\uu get()
重载功能:

function get_settings($varname){
    return $this->settings[$varname];
}
公共函数获取($name){ 返回$this->settings[$name]; }

这样称呼它:

$class->varname


(类中不存在的函数/变量,将被发送到get()重载函数

您可以始终重载神奇函数:

<?php

class DynamicSettings
{
    /**
     * Stores the settings
     * @var array
     **/
    protected $settings = array();

    /**
     * Called when something like this: 
     *  $dynset->name = value;
     * is executed.
     **/
    public function __set($name, $value)
    {
        $this->settings[$name] = $value;
    }

    /**
     * Called when something like this: 
     *  $value = $dynset->name;
     * is executed.
     **/
    public function __get($name)
    {
        if (array_key_exists($name, $this->settings)) 
        {
            return $this->data[$name];
        }
        $trace = debug_backtrace();
        trigger_error('Undefined dynamic property ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    /**
     * Called when checking for variable existance
     **/
    public function __isset($name)
    {
        return isset($this->settings[$name]);
    }

    /**
     * Called when unsetting some value.
     **/
    public function __unset($name)
    {
        unset($this->settings[$name]);
    }

}

$dynset = new DynamicSettings();
$dynset->hello = "Hello "; // creates array key "hello" with value "Hello "
$dynset->world = "World!"; // creates array key "world" with value "World!"

echo $dynset->hello . $dynset->world; // outputs "Hello World!"

您始终可以重载魔术功能:

<?php

class DynamicSettings
{
    /**
     * Stores the settings
     * @var array
     **/
    protected $settings = array();

    /**
     * Called when something like this: 
     *  $dynset->name = value;
     * is executed.
     **/
    public function __set($name, $value)
    {
        $this->settings[$name] = $value;
    }

    /**
     * Called when something like this: 
     *  $value = $dynset->name;
     * is executed.
     **/
    public function __get($name)
    {
        if (array_key_exists($name, $this->settings)) 
        {
            return $this->data[$name];
        }
        $trace = debug_backtrace();
        trigger_error('Undefined dynamic property ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    /**
     * Called when checking for variable existance
     **/
    public function __isset($name)
    {
        return isset($this->settings[$name]);
    }

    /**
     * Called when unsetting some value.
     **/
    public function __unset($name)
    {
        unset($this->settings[$name]);
    }

}

$dynset = new DynamicSettings();
$dynset->hello = "Hello "; // creates array key "hello" with value "Hello "
$dynset->world = "World!"; // creates array key "world" with value "World!"

echo $dynset->hello . $dynset->world; // outputs "Hello World!"

非常感谢,这正是我想要的!:)丢弃变量名称空间通常不是一个好主意,即使您可以控制要提取的内容。这当然完全符合OP的要求(+1),但我想对此发表评论:
extract
在更大的代码库中,这使得很难看到变量的实例化/来源,所以在我个人看来,我只会谨慎地使用它。@MarcB非常同意,我在答案中添加了一个注释。非常感谢,这正是我想要的r!:)通常情况下,乱扔变量名称空间不是一个好主意,即使您可以控制提取的内容。这当然可以完美地满足OP的要求(+1),但我想对此进行评论:
extract
在更大的代码库中会使查看变量实例化/来自何处变得更加困难,所以在我个人看来,我只会谨慎地使用它。@MarcB非常同意,我在答案中添加了一个注释。