为什么这甚至是一个类?或者为什么这些函数至少不是静态的?

发布于 2024-11-01 13:56:40 字数 692 浏览 3 评论 0原文

我一直在努力学习 PHP,并且在制作自己的博客引擎方面进展顺利。当需要集成 OAuth 时,我遇到了 这个加密密钥的解决方案

用法说要做一些这样的事情:

<?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";

?>

我的问题是......为什么这是一个类?看起来两个功能就可以了。我真的不明白为什么我要实例化一个对象然后调用一些方法。这是 OOP 思维失控的一个例子,还是我在这里遗漏了什么?

如果有一些令人信服的理由让它成为一个类,为什么这些方法不是静态的,以便我可以调用 proCrypt::encrypt( 'my message' ); ?

这是相关的,因为我编写的许多代码都使用静态函数,或者使用函数式编程而不是 OOP。如果我做错了什么,我想知道。

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 something 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏九 2024-11-08 13:56:40

该类有一些可以设置的变量来影响加密的结果。如果您要将此类设为静态,则只需设置这些变量一次,使用该函数的每个人都会受到影响。相反,如果将其设为对象,则可以轻松创建具有不同值的多个版本。

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.

一瞬间的火花 2024-11-08 13:56:40

也许是因为某些加密算法需要一些额外的状态作为输入(如公钥/私钥),并且由对象封装。

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

梦初启 2024-11-08 13:56:40

一种可能性:“记忆”。

类在这里可能很有用,因为它可能保留中间结果或缓存以前的结果。

这不是“OOP 思维失控”。这只是谨慎的设计,因为——也许——幕后正在发生一些有状态的事情。

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.

有深☉意 2024-11-08 13:56:40

好吧,我不确定为什么您找到的解决方案不是静态的。

我已经开始使用 这个解决方案,我在堆栈上找到了该解决方案,该解决方案在静态中调用方式

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文