【发布时间】:2015-09-15 14:15:05
【问题描述】:
这是关于代码的可读性:
我已经处理这个问题很长时间了,我一直想知道处理传递参数的最佳方法是什么。
很多时候,阅读其他程序员的代码我发现这样的行:
$Instance->functionCall('Abc123', 5, 1.24, 'XYZ', 642);
这让我不得不去 Class 文件看看那些参数的含义。
我尽我所能通过这样做来编写可读的代码:
$user_name = 'Abc123';
$age = 5;
$height = 1.24;
$hobbies = 'XYZ';
$num_brothers = 642;
$Instance->functionCall($user_name, $age, $height, $hobbies, $num_brothers);
或者这个:
$Instance->functionCall($user_name = 'Abc123', $age = 5, $height = 1.24, $hobbies = 'XYZ', $num_brothers = 642);
但是这个变量占用内存,没有在其他地方使用。 我喜欢认为这个“丢失”的内存空间是值得的,因为它更具可读性,但我想知道是否有更好的方法。
有什么想法吗?
谢谢大家!
【问题讨论】:
-
对 phpdoc 使用第一种方法(方法和变量的文档),一切顺利 - 您的 IDE 会告诉您需要了解的内容
标签: php function parameters conventions readability