Unlimited Usenet
day retention, 99% Completion, Unlimited Access, Free Trial!

Use of Complex (Curly Bracket) Syntax

This is a discussion on Use of Complex (Curly Bracket) Syntax within the PHP Language forums, part of the PHP Programming Forums category; If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-04-2005
Ken in Melbourne Australia
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 03-04-2005
Malcolm Dew-Jones
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

Ken in Melbourne Australia (ken@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.
Reply With Quote
  #3 (permalink)  
Old 03-04-2005
NSpam
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

Malcolm Dew-Jones wrote:
> Ken in Melbourne Australia (ken@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
Reply With Quote
  #4 (permalink)  
Old 03-04-2005
Michael Fesser
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

.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
Reply With Quote
  #5 (permalink)  
Old 03-05-2005
Chung Leong
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

"Ken in Melbourne Australia" <ken@mira.net> wrote in message
news:4227cb6a$0$5466$5a62ac22@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.


Reply With Quote
  #6 (permalink)  
Old 03-05-2005
Joshua Beall
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

> 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!?


Reply With Quote
  #7 (permalink)  
Old 03-06-2005
Michael Fesser
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

.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
Reply With Quote
  #8 (permalink)  
Old 03-07-2005
Chung Leong
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

"Michael Fesser" <netizen@gmx.net> wrote in message
news:r8om21158l1ik1o7k6fpbn69k4plbbsmpf@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.


Reply With Quote
  #9 (permalink)  
Old 03-07-2005
Jerry Stuckle
 
Posts: n/a
Default Re: Use of Complex (Curly Bracket) Syntax

Chung Leong wrote:
>
> "Michael Fesser" <netizen@gmx.net> wrote in message
> news:r8om21158l1ik1o7k6fpbn69k4plbbsmpf@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.
jstucklex@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0