GenerativeAiResult::__clone()

In this article

Performs a deep clone of the result.

Description

This method ensures that all nested objects (candidates, token usage, metadata) are cloned to prevent modifications to the cloned result from affecting the original.

Source

public function __clone()
{
    $clonedCandidates = [];
    foreach ($this->candidates as $candidate) {
        $clonedCandidates[] = clone $candidate;
    }
    $this->candidates = $clonedCandidates;
    $this->tokenUsage = clone $this->tokenUsage;
    $this->providerMetadata = clone $this->providerMetadata;
    $this->modelMetadata = clone $this->modelMetadata;
}

Changelog

VersionDescription
0.4.2Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.