Closed bryglen closed 8 years ago
toString only works when you are doing 'echo'
But when you store it on the db or pass it into the 3rd party api, it will save as a object and not the magic method __toString itself
such examples are
$api->post(['description'=> s('some_long_description')->truncate(10)])
This method return stringify object instead of the truncated string.
Sent from my iPad
On 14 Apr 2016, at 11:55 PM, Daniel St. Jules notifications@github.com wrote:
But why? Would it just be an alias for __toString? Can you show any examples in which it would be necessary?
— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub
That's not true. It works with a lot of functions that act on strings. You can also cast using (string) $strObject
Ive also try to cast it as a string but it was still giving object,
Try it like this
json_encode(['description'=>s('longlong')->truncate(200)]); and save to the DB, you will see the description will turn as empty {}
I'm working something and used it like that, when i try to echo it works fine but when i try to save it to the db or to the API it will save as an object.
On 15 Apr 2016, at 12:12 AM, Daniel St. Jules notifications@github.com wrote:
That's not true. It works with a lot of functions that act on strings. You can also cast using (string) $strObject
— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub
I can make PR if you want.. I am really 100percent sure that it doesn't work when passing to thr API or saving directly to the db without echo.. I am using php 5.6 btw.
On 15 Apr 2016, at 12:12 AM, Daniel St. Jules notifications@github.com wrote:
That's not true. It works with a lot of functions that act on strings. You can also cast using (string) $strObject
— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub
var_dump(json_encode([
'description' => (string) s('longlong')->truncate(200)
]));
// => string(26) "{"description":"longlong"}"
At that point,
// it's whether or not this
s('longlong')->truncate(200)->getString();
// looks nicer than this
(string) s('longlong')->truncate(200);
Okay, if typecasting will work for now. that would be great. I will test again on my end and see if its still an issue. thanks
But why? Would it just be an alias for
__toString
? Can you show any examples in which it would be necessary?