Pro get/set
当一个类有很多方法时,最好使用动词前缀,如get/set,以区分各个方法。
PHP示例:
$foo->setText('Hello world!');
$foo->prependText('So. ');
$foo->appendText(' And welcome');
$x = $foo->getText();
顺便提一下,在匈牙利命名法中,前缀使用小写字母,并且不会影响关键字。
计数器的获取/设置
当您只需要两种方法时,最好在使用参数的上下文中使用相同的名词。
jQuery示例:
$('.foo').html(); //get
$('.foo').html('Hello world!'); //set
示例
对于带有数组参数的函数和静态方法,我使用以下规则:
如果只应在运行时发生更改:
setFoo($arr)
setFoo([])
setFoo()
delFoo($arr)
addFoo($arr)
如果更改将永久保存(在数据库或文件中):
deleteFoo(...)
insertFoo(...)
replaceFoo(...)
updateFoo(...)
对于这两种情况:
$arr = getFoo(); // Get all properties
$val = getFoo($level1, $level2, ...); // You can obtain the value of the given level, placing the list of arguments
or
$val=getFoo()[$level1][$level2];