News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Lukas Smith:
__toString() or not __toString()?
August 28, 2013 @ 09:14:39

In Lukas Smith's latest post he looks at one of the magic methods that's built into PHP to help transform objects into strings - the __toString method. In the post he explores what it's for and what it might be used for.

The __toString() belongs to the family of methods and functions called "magic functions". They are magic because for the most part they do not get called explicitly but rather intercept operations. Unfortunately there are limits to its magic, specifically the only "context" the method is aware of is its design contract: to return a string. But its not clear what purpose that is. Should this be for some internal debugging or logging purposes? There one would be most interested in internal identifiers and object state. Is it for some frontend UI where the user will most likely be interested in some textual identifier that isn't too long as to not clutter the UI. There in lies the dilemma in the magic, while useful there is no way to ensure that the given context is passed on.

He looks at use cases for UI output (for consistent output) and contexts as well as the more internal-facing usage, like for logging and debugging purposes. Being able to get the context of the object as a string and pushed into a log at the time of error could be immensely helpful for debugging. He also links to some discussions happening on Twitter about the internal versus external uses of this magic method.

0 comments voice your opinion now!
tostring magicmethod usecase internal external

Link: http://pooteeweet.org/blog/0/2231#m2231

Rasmus Larsson:
Building a template engine in PHP
May 31, 2013 @ 11:11:27

Rasmus Larsson has a recent post to his site showing how to build a basic templating engine in PHP that uses the "__toString" to help render the result.

Possibly the most common sign of bad code is tight coupling, especially between logic and presentation. It might seem like a good idea to print out the HTML while the data is being generated, but it more often than not leads to a big incoherent mess of tangled code. [...] While PHP makes it unnecessarily easy to write shitty code it also provides a lot of ways to avoid it. I'll use this post to show you how ridiculously easy it is to create a template engine in PHP in three easy steps.

He includes the sample code for a "Template" class and shows the combination of exporting variables and output buffering to return the resulting template populated with values. The "__toString" method makes it so that you can just echo out the object and get the results.

0 comments voice your opinion now!
template engine tutorial simple tostring view

Link: http://www.rasmuslarsson.se/2013/05/a-template-engine-in-php

Refulz.com:
The __toString() Method - Objects as Strings
February 09, 2012 @ 09:27:19

On the Refulz.com blog there's a recent post introducing the __toString() magic method in PHP. This handy method allows you to define how to return an object when it's referenced as a string.

We started the study of PHP magic methods by learning about __get() magic method. [...] PHP is loosely typed language and same variable can be used or referred as string, number or object. The __toString() method is called when the code attempts to treat an object like a string. This function does not accept any arguments and should return a string.

Some quick code is included showing how it works - returning a combined string made from two private class properties when the object ($obj) is echoed out. They also show multiple ways of using the method in both pre- and post-PHP 5.2.

0 comments voice your opinion now!
tostring magic method object string


Micheal Kimsal's Blog:
Symfony __toString() generation
August 20, 2008 @ 11:14:50

Michael Kimsal has pointed out a small irritation when using the Symfony framework and models - an issue when using models that have relationships.

If there are relations (an Author has a Book, for example). the generated forms will complain that the generated Models need a __toString() method to be used in the Form/View. In grails, this is the case, but every domain (corresponding to a Symfony 'model') has an implicit toString() method already generated, which return the string ":". For most production work, you'll want to override it with whatever you need the string to read, but for prototyping, it's fine.

He went in and modified the Symfony core to add in a __toString call that would return the object correctly. Several of the commentors agree with his frustration and some of the Symfony developers even chime in with some of the reasoning behind why it's like that.

0 comments voice your opinion now!
symfony tostring generation model issue join


Zend Developer Zone:
PHP Gotchas!
April 24, 2006 @ 12:49:58

The Zend Developer Zone has a new post for those out there struggling with the small stuff. You've got the language down and you're learning the syntax, but there's still a few things that elude your grasp. If this is you, check out their list of "PHP Gotchas" to see if your problem is on there.

Call them obscure, call them pointless, call them "newb mistakes." Whatever you call them, you've more than likely been tripped up at some point in your PHP coding journey by seemingly odd or illogical behaviors of the language. With PHP being a loosely-typed language, funny things are bound to happen.

PHP is an easy language to pick up for the casual coder--things should "just work." But not everyone comes into PHP development with a strong programming background, so here are some charming examples of ways PHP can trip you up if you aren't careful. Put on your thinking caps--here comes the science!

Included in their list of common problems for budding PHP developers are things like finding a "needle" in a string "haystack", working with constants, using and/or, and the __toString functionality in PHP5.

0 comments voice your opinion now!
gotchas strings constants tostring logic math gotchas strings constants tostring logic math



Community Events





Don't see your event here?
Let us know!


podcast application package install composer framework community symfony tool security introduction library version interview series opinion laravel release voicesoftheelephpant language

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework