PHP:防止链式方法返回?

发布于 2024-10-18 17:30:33 字数 338 浏览 4 评论 0原文

对于一个返回值的相当简单的 PHP 类的方法链接,我有些头疼,有时需要经历解密过程:

$dataset = new Datacontainer;
$key = $dataset->get('key');
$key2 = $dataset->get('key')->decrypt();

get 方法是返回值所在的地方。因此,对第二行的 decrypt 方法的调用在当前状态下不起作用。

我可以做一些事情来设置 get 方法仅在没有任何链接时才返回,或者重构此代码的最佳方法是什么?

I am having some headaches regarding method chaining for a quite simple PHP class that returns a value, which sometimes need to go through a decryption process:

$dataset = new Datacontainer;
$key = $dataset->get('key');
$key2 = $dataset->get('key')->decrypt();

The get method is where the return lives. So the call to the decrypt method on the second row isn't going to work in its current state.

Can I do something to setup the get method to return only when nothing is chained to it, or what would be the best way to re-factor this code?

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

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

发布评论

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

评论(4

时光病人 2024-10-25 17:30:33

get() 方法实际上并不知道是否有任何东西链接到它;但如果 get() 方法不返回任何内容(null),PHP 将抱怨尝试对非对象调用 decrypt() 方法。

您可以做的是将一个附加参数传递到 get() 方法中,指示它是否应该返回一个值,或者使用解密方法返回对象。

$key = $dataset->get('key');
$key2 = $dataset->get('key',true)->decrypt();

The get() method doesn't actually know whether anything is chained to it or not; but if the get() method returns nothing (null) PHP will complain about the attempt to call the decrypt() method on a non-object.

What you could do is pass an additional argument into the get() method that indicates whether it should return a value, or the object with the decrypt method.

$key = $dataset->get('key');
$key2 = $dataset->get('key',true)->decrypt();
深者入戏 2024-10-25 17:30:33

我可以做一些事情来设置 get
仅当没有任何内容时才返回的方法
拴在它身上...?

不,那是不可能的。 (也许使用高度复杂的反射技术,但如果可能的话,这是不切实际的。)

或者什么是最好的方法
重构这段代码?

我认为你们的班级结构有问题。通常,执行某些操作但返回工作实例的方法会更改类/实例的状态,例如通过属性,该属性又可以通过特殊/getter 方法获取。

Can I do something to setup the get
method to return only when nothing is
chained to it ... ?

No. That's not possible. (Maybe using highly complicated reflection-technology, but that's not practical, if possible at all.)

or what would be the best way to
re-factor this code?

I think there is something wrong with the structure of your class. Usually a method that does something but returns the working-instance, changes the state of the class/instance, e.g. through an attribute, that again can be fetched through a special/getter method.

ζ澈沫 2024-10-25 17:30:33

$dataset->get('key') 必须返回一个对象,其中 decrypt() 是一个方法。目前尚不清楚您的 decrypt() 方法属于哪个类。如果它是 $dataset 类的一部分,那么您需要用两行调用它:

$key2_encrypted = $dataset->get('key');
$key2 = $dataset->decrypt($key2_encrypted);

$dataset->get('key') has to return an object in which decrypt() is a method. It isn't clear what class your decrypt() method is a part of. If it's a part of your $dataset class then you you need to call it in two lines:

$key2_encrypted = $dataset->get('key');
$key2 = $dataset->decrypt($key2_encrypted);
你如我软肋 2024-10-25 17:30:33

根据我目前对该过程的理解,我将采取以下方法:

解密数据的对象正在提供服务。 传入

此类对象通常作为协作者通过构造函数

class ClientClass {
    private $decryptor
    public function __construct(Decryptor $decryptor) {
        $this->decryptor = $decryptor;
    }
    public function doSomethingWith(DataSet $data) {
        $key = $DataSet->getKey();
        // do stuff, decide whether there are encryption needs
        $encryptedKey = $this->decryptor->decrypt($key);
    }
}

:请注意,这里有改进的空间,从数据集中获取密钥(然后我需要更多地了解正在完成的工作,因此名称为 ClientClass) 。

Based on my current understanding of the process I would take the following approach:

An object that decrypts data is providing a service. Such objects are most often passed in

via the constructor as a collaborator:

class ClientClass {
    private $decryptor
    public function __construct(Decryptor $decryptor) {
        $this->decryptor = $decryptor;
    }
    public function doSomethingWith(DataSet $data) {
        $key = $DataSet->getKey();
        // do stuff, decide whether there are encryption needs
        $encryptedKey = $this->decryptor->decrypt($key);
    }
}

Note that there is room for improvement here with the getting of the key from the dataset (then I would need to know more about what is being accomplished, hence the name ClientClass).

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