dongya6395 2018-07-27 11:47
浏览 28

使用数组作为函数参数的最佳实践是什么?

I'm an intermediate programmer who works with PHP mostly. Recently, I noticed that many PHP developers use arrays as function arguments in their code. I'm little sceptic of using arrays like that, as some of the people in my workplace said that it's not the best practice.

For example, I have this piece of code in one of my WordPress projects right now, it might not make sense to you completely, but it's relevant:

<?php
/**
 * Print a list of featured posts.
 *
 * @param  $args Array  Function Arguments
 * @return $html String The final markup
 */
if( function_exists( 'abc_featured_posts_list' ) ) :
  function abc_featured_posts_list( $args = array(
    'number'  => 8,
    'image'   => 'path/to/default.image',
    'wrapper' => false,
    'style'   => 'minimal'
  ) ) {
    // Storage for the list markup
    $html = null;

    ...
    ...

    return html;
  }
endif;  
?>

It's the first time I'm passing arguments like this in a PHP function. Is it the right way to do that? What is the advantage of using an array over a bunch of variables in such a case?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏