SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
Thread: Method Arguments vs more methods
-
May 25, 2009, 01:10 #1
Method Arguments vs more methods
Hi,
what are your preferences, what would you recommend and why?
For example:
SetFile($Arg1, $Arg2, $Arg3, $Optional);
Or the other setup:
SetFile($Arg1, $Arg2, $Arg3);
SetOptionalFile($Arg1, $Arg2, $Arg3) {
if (!empty()) $this -> SetFile($Arg1, $Arg2, $Arg3);
Return TRUE;
}
Thanks
-
May 25, 2009, 01:19 #2
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I would much prefer the two-functions approach, because it makes code more readable. When you see the client code, it's obvious. Compare:
PHP Code:$foo->setFile('blah', 42, 'stuff', true);
PHP Code:$foo->SetOptionalFile('blah', 42, 'stuff');
-
May 25, 2009, 01:40 #3
- Join Date
- Mar 2007
- Location
- Czech Republic
- Posts
- 375
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There is also third setup:
PHP Code:function SetFile() {
switch func_num_args() {
......
}
}
-
May 25, 2009, 02:06 #4
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Assuming that the arguments represent properties of a file I would create a class to represent the file. Then I would use individual setters for each property of the file. This would be separate from the class that handles the file object. From your example that seems like what your trying to accomplish. On a generic level the decision can't be made without knowing the purpose of the method(s). I always make this decision based on scope and purpose.
PHP Code:class File {
protected $mime;
protected $name;
protected $temp;
public function setName($name) {
$this->name = $name;
}
public function setMime($mime) {
$this->mime = $mime;
}
public function setTemp($temp) {
$this->temp = $temp;
}
}
class FileManager {
protected $file;
public function setFile(File $file) {
$this->file = $file;
}
}
-
May 25, 2009, 07:41 #5
- Join Date
- Jun 2006
- Posts
- 638
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What's wrong with:
PHP Code:// $foo->setFile( file_name, max_size, text_body, [file lock, default false]. [special case, default false] );
$foo->setFile('blah', 42, 'stuff');
$foo->setFile('blah', 42, 'stuff', true);
$foo->setFile('blah', 42, 'stuff', true, false);
PHP Code:$myFile = new SpecialFile();
// $foo->setFile(SpecialFile $file)
$foo->setFile($myFile);
-
May 25, 2009, 08:14 #6
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 25, 2009, 08:48 #7
- Join Date
- Jun 2006
- Posts
- 638
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your not going to use setters/getters for every function you make in your project just so you can tell what they are for.
There are 2 ways to know what they are:
#1 search the documentation (yes, i know, i hate it also)
#2 use a good IDE that searches the documentation for you, and as you type, it will tell you. Some good IDEs will tell you what the parameters are from your own comments (custom functions), and even go to that function's declaration if you control+click that function.
-
May 25, 2009, 14:49 #8
- Join Date
- Dec 2002
- Location
- Ann Arbor, MI (USA)
- Posts
- 648
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 25, 2009, 23:50 #9
The reason why I came up with this question is because I was going through some old code of mine. I used to cram as much possibilities into one function.
Which ended up in a few boolean arguments... But I thought that was the way to go, showing what I could do...
Going through that code again afterwards I had to go and search my function lists to figure out what exactly was going on in there. That's why I suddenly started doubting. Code I wrote less than a year ago had become annoyingly unreadable.
Apparently the struggle in my own brain is also a point of discussion between people and from what I can see it really comes down to personal preference and there is no general rule towards this.
There is also the way of making boolean arguments more readable like:
PHP Code:<?php
define('OPTIONAL', TRUE);
$foo -> setfile('blah', 42, 'stuff', OPTIONAL);
?>
Thanks for all the reactions people
EDIT:
It is actually a function in my form handling class to upload file uploadsLast edited by fristi; May 25, 2009 at 23:52. Reason: added quote
-
May 26, 2009, 00:51 #10
Well you don't have to 'dirty up' your code with define statements.
If you are using objects and PHP 5 can use Class Constants.
PHP Code:class TestThis
{
const
ONE_CONST = 1,
TWO_CONST = 2;
protected
$one_var = 1,
$two_var = 2;
}
-
May 26, 2009, 02:24 #11
Yes, But that only works when using the methods in another class right?
If I'm to use this in a "main" script, the only way to go is to use define.
Or am I wrong?
Using:
PHP Code:<?php
$foo -> setfile('blah', 42, 'stuff', GlobalClass::OPTIONAL);
?>
But please correct me if I'm wrong, I'm still learning
-
May 26, 2009, 04:03 #12
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Well, out of the examples given so far, Ezku's is by far the easiest to understand.
It would be implemented like:
PHP Code:class File{
protected $Name = ''
, $MaxSize = 64
, $TextBody = ''
, $FileLock = false
, $SpecialCase = false
;
public function setName($Name){ $this->Name = $Name; return $this;}
public function getName(){ return $this->Name; }
public function setMaxSize($MaxSize){ $this->MaxSize = (int)$MaxSize; return $this; }
public function getMaxSize(){ return $this->MaxSize; }
public function setTextBody($TextBody){ $this->TextBody = $TextBody; return $this; }
public function getTextBody(){ return $this->TextBody; }
public function setFileLock($FileLock){ $this->FileLock = (bool)$FileLock; return $this; }
public function isFileLock(){ return $this->FileLock; }
public function setSpecialCase($SpecialCase){ $this->SpecialCase = (bool)$SpecialCase; }
public function isSpecialCase(){ return $this->SpecialCase; }
public function enableFileLock(){
return $this->setFileLock(true);
}
public function disableFileLock(){
return $this->setFileLock(false);
}
public function enableSpecialCase(){
return $this->setSpecialCase(true);
}
public function disableSpecialCase(){
return $this->setSpecialCase(false);
}
}
$File = new File();
$File->setName('blah')
->setMaxSize(42)
->setTextBody('stuff')
->enableFileLock()
->disableSpecialCase();
$Foo->SetFile($File);
Last edited by Jake Arkinstall; May 26, 2009 at 07:00.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 26, 2009, 04:19 #13
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 26, 2009, 06:11 #14
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Actually, no! (
) I've been doing quite a bit of C++, and because of the way that you lay methods out (simply listing them in the class definition and actually defining them in a source file), it's tempting to add more and more methods for common functionality. I suppose previous Java programming may have had some role there.
In fact, it's been very useful. It's amazing how much quicker writing applications is once you have written methods for many common tasks.
Of course, the enableFileLock/disableFileLock is only useful when it's certain what you want to do, which is why the other setFileLock is available for when you want to set it depending on an outcome.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
May 26, 2009, 06:21 #15
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I meant .. You forget $this->
-
May 26, 2009, 06:59 #16
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Ah, ok then!
Will update it now. Damn, I really need to pay more attention!Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Bookmarks