开发者

Why is this even a class? Or why aren't the functions at least static?

I've been trying to learn PHP and I'm progressing pretty well at making my own blog engine. When it came time to integrate OAuth, I came across this solution to encrypt keys.

The usage says to do something along these lines:

<?php

// a new proCrypt instance
$crypt = new proCrypt;

// encrypt the string
$encoded = $crypt->encrypt( 'my message');
echo $encoded."\n";

// decrypt the string
echo $crypt->decrypt( $encoded ) . "\n";

?>

My question is... why is this a class? It seems like two functions would be just fine. I don't really get why I'd instantiate an object and then call some methods. Is this an example of OOP thinking run amok, or is there somethi开发者_JAVA技巧ng I'm missing here?

If there is some compelling reason for it to be a class, why aren't the methods static so that I could just call proCrypt::encrypt( 'my message' );?

This is relavent as a lot of the code I've written has been using static functions, or stand along functional programming instead of OOP. If I'm doing something horribly wrong, I'd like to know about it.


The class has some variables that can be set as to affect the outcome of the encryption. If you were to make this class static, you would set these variables once and the everyone who used that function would be affected. Instead if you make it an object, it is easy to create multiple versions with different values.


Maybe because some encryption algorithms need some additional state as input (like a public/private key), and that's encapsulated by the object.


One possibility: "memoization".

A class might be useful here because it might retain intermediate results or cache previous results.

That's not "OOP thinking run amok". It's just prudent design because -- perhaps -- there's something stateful going on behind the scenes.


Well i'm not sure why that solution you found isn't static.

I have started to use this solution which i found on stack which is called in a static way

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜