Skip to content

Conversation

@sbulen
Copy link
Contributor

@sbulen sbulen commented Oct 9, 2024

Fixes #8306

Cannot use the object itself as an array key. Using the __toString() method instead wherever used as a key.

Tests OK.

@sbulen sbulen changed the title Use __toString for keys, not objs [3.0] Use __toString() for keys, not objs Oct 9, 2024
@live627
Copy link
Contributor

live627 commented Nov 1, 2024

The explicit calls to that magic method makes the code look funny. Since that routine appears to work only with strings, could you instead change new Ip(/* ... */) to either new Ip(/* ... */)->ip or (string) new Ip(/* ... */)?

@sbulen
Copy link
Contributor Author

sbulen commented Nov 9, 2024

Found a few spots I missed.

The explicit calls to that magic method makes the code look funny. Since that routine appears to work only with strings, could you instead change new Ip(/* ... */) to either new Ip(/* ... */)->ip or (string) new Ip(/* ... */)?

I looked into this, & considered using the existing ->expand() method, but then some of the comparisons/dupe checks would fail.

I also considered adding an ->ip() method, but that would be inconsistent with the other classes we have, which explicitly define __toString() for string conversion such as this.

@Sesquipedalian
Copy link
Member

Sesquipedalian commented Nov 10, 2024

Just cast to string. Not only is that the simplest and most concise approach, but it also communicates the most information to developers reading the code later. When we see an object being cast to a string, we know we are getting a string and that the object must implement the Stringable interface, and we know it without needing to look up property types or method return types.

@sbulen sbulen changed the title [3.0] Use __toString() for keys, not objs [3.0] Cast objs to string when used as keys Nov 11, 2024
@sbulen
Copy link
Contributor Author

sbulen commented Nov 11, 2024

Done & retested.

It's a nit, but, something bugs me about requiring a type cast, it seems inconsistent, e.g., IP.php has toHex() and toBinary(), but instead of providing a similar toString(), we require a cast instead. Requiring a cast on our own object feels like we missed something to me.

Then again, there aren't magic methods for __hex() & __binary()...

And as long as we're consistent going forward, we're good.

@Sesquipedalian
Copy link
Member

Well, it's not that we require
a cast. Calling __toString() works. It's just a style preference that the majority of us share.

Personally, I wish PHP would add some more magic methods for casting to scalar values (e.g. __toInt(), __toFloat(), etc.) so that developers could use this approach more broadly for the reasons stated above.

@Sesquipedalian Sesquipedalian merged commit 32449bc into SimpleMachines:release-3.0 Nov 11, 2024
@sbulen sbulen deleted the 30_ip_obj_key branch November 11, 2024 21:00
@jdarwood007
Copy link
Member

To be fair, in my opinion, our force casting should be fixed in the future. For 3.0, using force casting is acceptable as rewriting lots to get the app into a app supported by singletons, so we can move to a more modern approach later that uses factories, compartmentalizes logic into smaller replaceable objects or other methods will be easier.

I would see the IP class as having a ToString(), ToInet, ToBinary, etc method to support use cases in our code.

@live627
Copy link
Contributor

live627 commented Nov 12, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[3.0]: New user signup WSOD - Uncaught TypeError: Cannot access offset of type SMF\IP on array

4 participants