473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 6814
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: deko | last post by:
Do I need to use curly brackets in PHP if .. else statements? other constructs? Does it matter? What are Best Practices? Why? thanks in advance... This seems to work WITHOUT curly brackets:...
2
by: Ken Fine | last post by:
I'm using XMLHTTP to screen-scrape many thousands of pages of content as part of a data-structuring project. One issue that I'm running into is that some entities such as curly quotes and curly...
6
by: STF | last post by:
While reading the C++ tutorial in this page: http://www.cplusplus.com/doc/tutorial/tut2-2.html I'm astonished to learn that we could omit curly brackets in function declaration for single...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
9
by: Christine | last post by:
It has come to my attention that sometimes, when I open a Query in SQL View, the SQL that I see is not exactly the same as the SQL in the Query's Querydef. The difference I see (only occasionally)...
4
by: Carl-Olof Almbladh | last post by:
Dear all, In C99, complex is now built in, and the C library contains functions for complex exp, etc. It is not so easy to find info about the syntax if one is not willing to buy the ISO/ANSI...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Zain Homer | last post by:
Someone please let me know if I'm sending this to the wrong email alias... I'm wondering why we can't use the glob module to glob with curly brackets like we can from the command line (at least...
3
by: Grande News | last post by:
Hi, I've got some PHP code that looks like this $cura= ord($data{$pnum*2}); $curb = ord($data{$pnum*2+1}); Notice the curly braces -- does using them make any difference, or will square...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.