开发者

PHP: Prevent chained method from returning?

开发者 https://www.devze.com 2023-02-12 09:02 出处:网络
I am having some headaches regarding method chaining for a quite simp开发者_运维知识库le PHP class that returns a value, which sometimes need to go through a decryption process:

I am having some headaches regarding method chaining for a quite simp开发者_运维知识库le 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?


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();


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.


$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);


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

0

精彩评论

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