469,283 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Use of Complex (Curly Bracket) Syntax

If I use the curly bracket syntax (referred to as the
complex syntax) within a string, how do I get to call a
function within it?

The php manual says that the first (or previous)
character for the curly bracket has to be a dollar sign '$'.
This is fine for variables, arrays and some objects but
doesn't allow me to call a function such as addslashes() or
trim() before I return the string in the variable.

I have tried using assignment, concatenation and even
methods of an instantiated object and have found the parser
refuses to allow the equal '=' sign, fullstop '.', or even a
standard left bracket '(' within the complex curly brackets.
For example, I had expected the following to work:

$x = "";
$string = <<<EOT
This is a text string ...
.... {$x . addslashes(trim($myinput1))} ...
.... {$x . addslashes(trim($myinput2))} ...
more text etc .....
EOT;

It doesn't work, the parser objects to the full stop symbol '.'

Worse still, the following was objected to

$string = "This is text .. {$x->myGetMethod($specifier)} ..
more text etc ";

The refusal to accept a left bracket here means that it is
not a real object oriented language as you can't always get
to objects within complex syntax!

Considering that they have called this the complex syntax, I
think that it is very poorly implemented. My expectation
was that any legal PHP expression should be able to be used
inside the braces so long as it resulted in a string. It is
far from that!

Strangely, I have read (but haven't tried) that the
following expression works:

$string = "This is text {${$result ? 'var1' : 'var2'}} ..
more text etc ";

Can anybody please tell me some way to use a function within
the curly bracket (complex) syntax? (without a discussion
on faults of Heredoc or doing it without the complex syntax)

Thanks
Ken

Jul 17 '05 #1
8 6553
Ken in Melbourne Australia (ke*@mira.net) wrote:
: If I use the curly bracket syntax (referred to as the
: complex syntax) within a string, how do I get to call a
: function within it?

: The php manual says that the first (or previous)
: character for the curly bracket has to be a dollar sign '$'.

http://ca3.php.net/types.string also says "you can include any value that
is in the namespace"

That doesn't sound to me like you can call a function, since the function
is not a value (instead it creates a new value and returns it, and the
result is not in any name space since it is not in a variable.)

Also, the examples don't show accessing functions with this syntax.
: This is fine for variables, arrays and some objects but

which is what the examples show

: doesn't allow me to call a function such as addslashes() or
: trim() before I return the string in the variable.

: I have tried using assignment, concatenation and even
: methods of an instantiated object and have found the parser
: refuses to allow the equal '=' sign, fullstop '.', or even a
: standard left bracket '(' within the complex curly brackets.
: For example, I had expected the following to work:

: $x = "";
: $string = <<<EOT
: This is a text string ...
: ... {$x . addslashes(trim($myinput1))} ...
: ... {$x . addslashes(trim($myinput2))} ...
: more text etc .....
: EOT;

: It doesn't work, the parser objects to the full stop symbol '.'

: Worse still, the following was objected to

: $string = "This is text .. {$x->myGetMethod($specifier)} ..
: more text etc ";

: The refusal to accept a left bracket here means that it is
: not a real object oriented language as you can't always get
: to objects within complex syntax!

I don't see that this has anything to do with php being (or not being) a
"real object oriented language". Consider for example java, which is
certainly a real object oriented language, but doesn't have this kind of
"embedding of variables within a string" capability as part of the
language at all.
: Considering that they have called this the complex syntax, I
: think that it is very poorly implemented. My expectation
: was that any legal PHP expression should be able to be used
: inside the braces so long as it resulted in a string. It is
: far from that!

: Strangely, I have read (but haven't tried) that the
: following expression works:

: $string = "This is text {${$result ? 'var1' : 'var2'}} ..
: more text etc ";

no idea, why don't you try it and see?

: Can anybody please tell me some way to use a function within
: the curly bracket (complex) syntax? (without a discussion
: on faults of Heredoc or doing it without the complex syntax)
--

This space not for rent.
Jul 17 '05 #2
Malcolm Dew-Jones wrote:
Ken in Melbourne Australia (ke*@mira.net) wrote:
: If I use the curly bracket syntax (referred to as the
: complex syntax) within a string, how do I get to call a
: function within it?

: The php manual says that the first (or previous)
: character for the curly bracket has to be a dollar sign '$'.

http://ca3.php.net/types.string also says "you can include any value that
is in the namespace"

That doesn't sound to me like you can call a function, since the function
is not a value (instead it creates a new value and returns it, and the
result is not in any name space since it is not in a variable.)

Also, the examples don't show accessing functions with this syntax.
: This is fine for variables, arrays and some objects but

which is what the examples show

: doesn't allow me to call a function such as addslashes() or
: trim() before I return the string in the variable.

: I have tried using assignment, concatenation and even
: methods of an instantiated object and have found the parser
: refuses to allow the equal '=' sign, fullstop '.', or even a
: standard left bracket '(' within the complex curly brackets.
: For example, I had expected the following to work:

: $x = "";
: $string = <<<EOT
: This is a text string ...
: ... {$x . addslashes(trim($myinput1))} ...
: ... {$x . addslashes(trim($myinput2))} ...
: more text etc .....
: EOT;

: It doesn't work, the parser objects to the full stop symbol '.'

: Worse still, the following was objected to

: $string = "This is text .. {$x->myGetMethod($specifier)} ..
: more text etc ";

: The refusal to accept a left bracket here means that it is
: not a real object oriented language as you can't always get
: to objects within complex syntax!

I don't see that this has anything to do with php being (or not being) a
"real object oriented language". Consider for example java, which is
certainly a real object oriented language, but doesn't have this kind of
"embedding of variables within a string" capability as part of the
language at all.
: Considering that they have called this the complex syntax, I
: think that it is very poorly implemented. My expectation
: was that any legal PHP expression should be able to be used
: inside the braces so long as it resulted in a string. It is
: far from that!

: Strangely, I have read (but haven't tried) that the
: following expression works:

: $string = "This is text {${$result ? 'var1' : 'var2'}} ..
: more text etc ";

no idea, why don't you try it and see?

: Can anybody please tell me some way to use a function within
: the curly bracket (complex) syntax? (without a discussion
: on faults of Heredoc or doing it without the complex syntax)
--

This space not for rent.

use the concatanation operator and force the language to do what you
want it do instead of relying upon the vagaries of the inline parser.
Its also more maintable and readable and its faster
Jul 17 '05 #3
.oO(Ken in Melbourne Australia)
The php manual says that the first (or previous)
character for the curly bracket has to be a dollar sign '$'.
This is fine for variables, arrays and some objects but
doesn't allow me to call a function such as addslashes() or
trim() before I return the string in the variable.
That's probably how it is.
The refusal to accept a left bracket here means that it is
not a real object oriented language as you can't always get
to objects within complex syntax!
What has curly syntax to do with OOP?
Can anybody please tell me some way to use a function within
the curly bracket (complex) syntax? (without a discussion
on faults of Heredoc or doing it without the complex syntax)


Why do you insist on using curly syntax? There are a dozen ways to do
what you want, if one way doesn't work why not simply use another? If I
have to apply functions to variables before putting them together in a
string I usually use sprintf().

Micha
Jul 17 '05 #4
"Ken in Melbourne Australia" <ke*@mira.net> wrote in message
news:42**********************@per-qv1-newsreader-01.iinet.net.au...
It doesn't work, the parser objects to the full stop symbol '.'

Worse still, the following was objected to

$string = "This is text .. {$x->myGetMethod($specifier)} ..
more text etc ";

The refusal to accept a left bracket here means that it is
not a real object oriented language as you can't always get
to objects within complex syntax!


It's true that PHP 4 is not a very OOP language. In PHP 5 there's support
for object property, which can be interpolate into strings.

Whenever I see people implement getter/setter functions in PHP, I don't know
if I should laugh or cry. Why on earth are you dragging that baggage from
the primitive wilderness of C++ into another language? You have to use
accessor function in C++ only because it doesn't support property. You don't
use them in a real OOP language like C# or Object Pascal.
Jul 17 '05 #5
> Whenever I see people implement getter/setter functions in PHP, I don't
know
if I should laugh or cry. Why on earth are you dragging that baggage from
the primitive wilderness of C++ into another language? You have to use
accessor function in C++ only because it doesn't support property. You
don't
use them in a real OOP language like C# or Object Pascal.


C++ "doesn't support property"? What C++ compiler are you using that does
not support object properties!?
Jul 17 '05 #6
.oO(Chung Leong)
Whenever I see people implement getter/setter functions in PHP, I don't know
if I should laugh or cry.
There's nothing wrong with that.
Why on earth are you dragging that baggage from
the primitive wilderness of C++ into another language?
It has nothing to do with C++ or any other particular language, it's
part of OOP in general. Communication with an object should be done by
using its public methods, member variables should be kept private.
You have to use
accessor function in C++ only because it doesn't support property. You don't
use them in a real OOP language like C# or Object Pascal.


Even the properties in OP are often mapped onto getter/setter functions.
In many cases a simple read/write access to a member variable is not
enough, sometimes additional checking has to be done before assigning a
new value or the returned data has to be calculated from something else.

Micha
Jul 17 '05 #7
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:r8********************************@4ax.com...
There's nothing wrong with that.
It's pointless to do it in PHP 4, where you cannot control access to object
variables. Calling a function to access a variable is slow. The fact that
method invocation triggers copy-on-write slows things down even more. And as
the OP found out, you can't interpolate a method call into a string.
Even the properties in OP are often mapped onto getter/setter functions.
In many cases a simple read/write access to a member variable is not
enough, sometimes additional checking has to be done before assigning a
new value or the returned data has to be calculated from something else.


All I am saying is that getter/setter functions are means to the end, and
not the end itself. In Object Pascal, you can map property to a variable or
a function (often times read-access -> variable, write-access -> function).
In C#, you use set/get blocks. In PHP 5, you have __set and __get, which
handle access of all properties of an object.
Jul 17 '05 #8
Chung Leong wrote:

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:r8********************************@4ax.com...
There's nothing wrong with that.


It's pointless to do it in PHP 4, where you cannot control access to object
variables. Calling a function to access a variable is slow. The fact that
method invocation triggers copy-on-write slows things down even more. And as
the OP found out, you can't interpolate a method call into a string.
Even the properties in OP are often mapped onto getter/setter functions.
In many cases a simple read/write access to a member variable is not
enough, sometimes additional checking has to be done before assigning a
new value or the returned data has to be calculated from something else.


All I am saying is that getter/setter functions are means to the end, and
not the end itself. In Object Pascal, you can map property to a variable or
a function (often times read-access -> variable, write-access -> function).
In C#, you use set/get blocks. In PHP 5, you have __set and __get, which
handle access of all properties of an object.

I disagree getters and setters are pointless. Rather, I maintain they
are an important part of OO programming.

Two reasons come to mind right away. First one is compatibility with
later releases of PHP. They way things are going, soon the private
keyword really will be private. That means you'll have to change all
pages which reference these variables.

The second reason is even bigger. One of the benefits of encapsulation
is that the rest of the program is not dependent upon the data itself.
OF course, there has to be some knowledge - but OO abstracts one layer.

An example. Let's say you have a database with Name in one column. You
get and fetch the value with a database access class, and access the
resultant variable directly.

Now your customer comes along and decides he needs to split this into
FirstName/LastName. Also, these separate variables must be available on
certain pages so he can alphabetize by last name. So, you need to save
them as separate variables in the database access class. Now you need
to change every page which uses name.

Go back and use getter and setter functions getName() and setName().
Change the database and change these two functions. Also add
get/setFirstName() and get/setLastName(). No further changes are
required to ANY other code.

More advanced topics include things like tracking when a variable
changes in the database access class so you know whether or not an
update needs to be done to the database (you can even tailor the update
to affect just the changed fields). Or you can have a "restore"
function to restore fields back to saved values.

Yes, encapsulation takes a little more time. But it's really not much,
and IMHO the benefits outweigh the overhead.

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by deko | last post: by
4 posts views Thread by Carl-Olof Almbladh | last post: by
2 posts views Thread by Zain Homer | last post: by
3 posts views Thread by Grande News | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.