如何将新变量与类中的默认变量进行匹配?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 array_merge php 函数。代码:
Try to use array_merge php function. Code: