[wp-trac] [WordPress Trac] #48565: The `site_url` function doesn't work with objects implemented `__toString`

WordPress Trac noreply at wordpress.org
Mon Nov 11 20:52:19 UTC 2019


#48565: The `site_url` function doesn't work with objects implemented  `__toString`
--------------------------+------------------------------
 Reporter:  azeemhassni   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:  5.2.4
 Severity:  trivial       |  Resolution:
 Keywords:  needs-patch   |     Focuses:
--------------------------+------------------------------

Comment (by azeemhassni):

 Yeah, I agree but I also think this should be addressed if possible
 because it doesn't feel good to call `__toString` directly because it
 meant be called by PHP?

 Though I don't feed really good about it but I suggest we should have a
 helper maybe called `wp_is_string` and replace `is_string` calls with
 that, If you think that's a good idea I will be more than happy to submit
 a patch.



 Replying to [comment:1 pbiron]:
 > Thanx for the ticket and welcome to Trac!
 >
 > That's because
 [https://developer.wordpress.org/reference/functions/get_site_url/
 get_site_url()] (which is called by `site_url()`) has an explicit check
 for whether the `$path` param is a string (see L3165).  If it's not a
 string, then it is ignored.
 >
 > While it would certainly be possible to add an additional check for
 whether `$path` is an object that has a `__toString()` method, why would
 that be desired?  If it were done in this one case, then it could be
 argued that it should be done for **ANY** "string" param to **ANY**
 function/method in core...and doing so doesn't seem reasonable to me.
 >
 > I'd suggest you just call `__toString()` yourself when you call
 `site_url()`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48565#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list