Fix collections property name conflict in Collections class #79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change Summary
What is this?
This change addresses a critical naming conflict in the Typesense PHP library where having a collection named "collections" causes an unhandled exception. The issue occurs because the
Collections
class has a property namedcollections
that conflicts with the magic getter method (__get
), causing it to return an empty array instead of the expected Collection object when accessing a collection named "collections".This fix is particularly important for users who might use "collections" as a legitimate collection name (e.g., in e-commerce systems), preventing unexpected behavior and improving the robustness of the library.
Changes
Code Changes:
src/Collections.php
:$collections
to$typesenseCollections
to avoid naming conflicts__get()
methodoffsetExists()
methodoffsetGet()
methodoffsetSet()
methodoffsetUnset()
methodContext
$this->typesense->getCollections()->{$collectionName}
$collectionName
is "collections"The fix maintains backward compatibility while resolving the naming conflict, allowing developers to use "collections" as a valid collection name in their applications.
PR Checklist