如何将新变量与类中的默认变量进行匹配?

发布于 2024-12-02 00:41:34 字数 1924 浏览 2 评论 0原文

在 jQuery 中有 $.extend() 函数。这基本上就像将默认设置与输入设置相匹配一样。好吧,我想在 PHP 中使用类似的技术,但似乎没有类似的功能。所以我想知道:是否有与 .extend() 类似的函数,但在 PHP 中?如果没有,还有什么替代方案? >

这是一个示例类,以及我当前如何获得此效果。我还添加了一条评论,我希望如何做到这一点:

class TestClass {
    // These are the default settings:
    var $settings = array(
        'show' => 10,
        'phrase' => 'Miley rocks!',
        'by' => 'Kalle H. Väravas',
        'version' => '1.0'
    );
    function __construct ($input_settings) {
        // This is how I would wish to do this:
        // $this->settings = extend($this->settings, $input_settings);

        // This is what I want to get rid of:
        $this->settings['show'] = empty($input_settings['show']) ? $this->settings['show'] : $input_settings['show'];
        $this->settings['phrase'] = empty($input_settings['phrase']) ? $this->settings['phrase'] : $input_settings['phrase'];
        $this->settings['by'] = empty($input_settings['by']) ? $this->settings['by'] : $input_settings['by'];
        $this->settings['version'] = empty($input_settings['version']) ? $this->settings['version'] : $input_settings['version'];

        // Obviously I cannot do anything neat or dynamical with $input_settings['totally_new'] :/
    }
    function Display () {
        // For simplifying purposes, lets use Display and not print_r the settings from construct
        return $this->settings;
    }
}

$new_settings = array(
    'show' => 30,
    'by' => 'Your name',
    'totally_new' => 'setting'
);

$TC = new TestClass($new_settings);

echo '<pre>'; print_r($TC->Display()); echo '</pre>';

如果您注意到,有一个全新的设置:$new_settings['totally_new']。它应该作为 $this->settings['totally_new'] 包含在数组中。 PS:上面的代码输出 this

In jQuery there is $.extend() function. That basically works like matching default settings with input settings. Well, I want to use similar technique in PHP, but it seems, that there is no similar function. So I'm wondering: Is there a similar function as .extend(), but in PHP? If not then, what alternatives are there?

This is an example class and how I get this effect currently. I also added a comment, how I would wish to do this:

class TestClass {
    // These are the default settings:
    var $settings = array(
        'show' => 10,
        'phrase' => 'Miley rocks!',
        'by' => 'Kalle H. Väravas',
        'version' => '1.0'
    );
    function __construct ($input_settings) {
        // This is how I would wish to do this:
        // $this->settings = extend($this->settings, $input_settings);

        // This is what I want to get rid of:
        $this->settings['show'] = empty($input_settings['show']) ? $this->settings['show'] : $input_settings['show'];
        $this->settings['phrase'] = empty($input_settings['phrase']) ? $this->settings['phrase'] : $input_settings['phrase'];
        $this->settings['by'] = empty($input_settings['by']) ? $this->settings['by'] : $input_settings['by'];
        $this->settings['version'] = empty($input_settings['version']) ? $this->settings['version'] : $input_settings['version'];

        // Obviously I cannot do anything neat or dynamical with $input_settings['totally_new'] :/
    }
    function Display () {
        // For simplifying purposes, lets use Display and not print_r the settings from construct
        return $this->settings;
    }
}

$new_settings = array(
    'show' => 30,
    'by' => 'Your name',
    'totally_new' => 'setting'
);

$TC = new TestClass($new_settings);

echo '<pre>'; print_r($TC->Display()); echo '</pre>';

If you notice, that there is a totally new setting: $new_settings['totally_new']. That should just get included inside the array as $this->settings['totally_new']. PS: Above code outputs this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-12-09 00:41:34

尝试使用 array_merge php 函数。代码:

array_merge($this->settings, $input_settings);

Try to use array_merge php function. Code:

array_merge($this->settings, $input_settings);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文