Re: Constants in strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2011-07-6 08:09, "admin@xxxxxxxxxxxxxxxxxxx"
<admin@xxxxxxxxxxxxxxxxxxx> wrote:



>I use constants in my OOP and I never use the heredoc syntax. Now I am
>fearing that I have not taken advantage of something.
>My understanding of heredoc syntax as of 5.3 is just a string quoting
>right?
>Is there an advantage of using the heredoc syntax over single quoted or
>double quoted?

I don't believe that a heredoc will perform significantly differently than
a double-quoted string, as, from the parser's POV, they're essentially the
same thing once you get past the step of extracting the entire string from
the source. I've not verified this by reviewing the relevant source,
however, nor have I benchmarked it. But, I'm willing to bit that even if
there is a difference, it's so small that you're better off worrying about
which will lead to easier code maintenance than worrying about performance
(as is typically the case with such micro-optimization choices).

In my view, what's important is how you use them. In particular, a heredoc
can present a bit more cleanly when you're dealing with a large-ish chunk
of text, as in, say, an e-mail message template. The main downside is that
they will usually make a mess of code formatting, since the closing
delimiter must be against the left margin. For this reason, I tend to
prefer multi-line double-quoted strings over heredocs when I have
meaningful indentation, as in a function or class method. Where I've made
most use of heredocs is when I want to do nothing but define a bunch of
long strings in one file. For example, I might create a file to define a
set of related e-mail message templates:

<?php

$accountCreationSuccessfulMessage = <<<EndSuccess
Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget,
tempor sit amet, ante.

Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae
est.

Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper
pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit
amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum
rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis
pulvinar facilisis. Ut felis.
EndSuccess



$accountCreationFailedMessage = <<<EndFailure
Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet
sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus.
Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non
lacus vitae ipsum viverra pretium. Phasellus massa.

Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce
consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim
adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum
nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius
nibh ut lacus. Curabitur fringilla.

Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a
quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat
urna, ut fermentum neque mi egestas dolor.
EndFailure



?>

Of course, this is arguably as clean:

<?php

$accountCreationSuccessfulMessage = "
Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget,
tempor sit amet, ante.

Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae
est.

Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper
pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit
amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum
rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis
pulvinar facilisis. Ut felis.
"; //$accountCreationSuccessfulMessage

$accountCreationFailedMessage = "
Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet
sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus.
Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non
lacus vitae ipsum viverra pretium. Phasellus massa.

Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce
consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim
adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum
nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius
nibh ut lacus. Curabitur fringilla.

Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a
quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat
urna, ut fermentum neque mi egestas dolor.
"; //$accountCreationFailedMessage

?>

With the latter, there is the catch that you end up with leading and
trailing line breaks, but those are easy enough to deal with, if desired.

As to the original topic of this thread, it's long annoyed me that there's
no easy way to use constants with interpolation. Since I find repeated
concatenation extremely ugly and prone to editing error (the same reason I
wouldn't use syntax any more complicated than {{foo}} even if it did
work), I end up taking the approach of defining a variable to use instead,
usually with a prefix to make clear that it's an understood constant
(e.g., $kUsername). And if I have to do this for one constant, I do it for
all related constants even if not needed, just for the sake of
consistency. This means that I frequently end up using variables where
constants would work just fine. With the prefix convention and a
disciplined team, this proves easily tenable, but it's not ideal.

-Bob


--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
http://www.thesba.com/


Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux