class with only static functions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I have no idea if it was the best route to take, however I personally dislike having multiple classes to perform the same operation on differing targets. It just feels cluttered to me.. but we're kinda wondering a bit off topic so I'll shut up unless the original poster would like for this conversation to continue (although I'm basically out of words at this point :lol:)
jonathant
Forum Commoner
Posts: 32
Joined: Sat Jan 07, 2006 3:13 pm

Post by jonathant »

I'm enjoying reading you guys debate. Unfortunately, I don't have enough experience to give an opinion one way or the other. But, I'm learning a lot from all your comments :D
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

The idea is to do this:

Code: Select all

//in a plugin file
$wgHasher =& new SHA256();
//$wgHasher =& new SHA512();

//in domain logic
$hash = $wgHasher->hash($text);
All you have to do is change the line in the plugin file, and voila, hashing system changed (of course, you'll still have to deal with old records using old hashes)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The question of different interfaces really depends on how the code around it would use this class. There is not really a right or wrong here. It is the difference between:

Code: Select all

//in a plugin file
$wgHasher =& new SHA256();

//in domain logic
$hash = $wgHasher->hashURL($text);
//$hash = $wgHasher->hashFile($text);
And:

Code: Select all

//in a plugin file
$wgHasher =& new SHA256URL();
//$wgHasher =& new SHA256File();

//in domain logic
$hash = $wgHasher->hash($text);
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, if $text is a URL, I might want to hash the URL, or I might want to hash the location it points to. There is little advantage to polymorphism between two functions that do wildly different things.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Ambush Commander wrote:Well, if $text is a URL, I might want to hash the URL, or I might want to hash the location it points to. There is little advantage to polymorphism between two functions that do wildly different things.
Right. The fact that they do "do wildly different things" would support that they should be different classes. There is a lot of commonality though. But my main point was that if they all have a hash() method that returns a hash, they can be polymorphic with regards to the classes that consume them. I can then refactor related code to be more generic. Again, the way I work may not be the way you work.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Why overly complicate things? Why create an entire object just for a hash?!

Code: Select all

$hash = SHA256::hash($text, 'URL'); //or 'file', or 'string' etc.. if you are wanting to use just one name
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jenk wrote:Why overly complicate things?
I don't think I was proposing complicating things. Though I'm not sure whether you are talking about he call to the class or the class itself.
Jenk wrote:Why create an entire object just for a hash?!
Why not? If you have looked through the code you would note that at least two "entire" objects are created by the static call you posted.
(#10850)
Post Reply