Use of Complex (Curly Bracket) Syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken in Melbourne Australia

    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($s pecifier)} ..
    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

  • Malcolm Dew-Jones

    #2
    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($s pecifier)} ..
    : 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.

    Comment

    • NSpam

      #3
      Re: Use of Complex (Curly Bracket) Syntax

      Malcolm Dew-Jones wrote:[color=blue]
      > 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($s pecifier)} ..
      > : 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.[/color]
      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

      Comment

      • Michael Fesser

        #4
        Re: Use of Complex (Curly Bracket) Syntax

        .oO(Ken in Melbourne Australia)
        [color=blue]
        > 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.[/color]

        That's probably how it is.
        [color=blue]
        >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![/color]

        What has curly syntax to do with OOP?
        [color=blue]
        >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)[/color]

        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

        Comment

        • Chung Leong

          #5
          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 ...[color=blue]
          > 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($s pecifier)} ..
          > 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![/color]

          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.


          Comment

          • Joshua Beall

            #6
            Re: Use of Complex (Curly Bracket) Syntax

            > Whenever I see people implement getter/setter functions in PHP, I don't[color=blue]
            > 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.[/color]

            C++ "doesn't support property"? What C++ compiler are you using that does
            not support object properties!?


            Comment

            • Michael Fesser

              #7
              Re: Use of Complex (Curly Bracket) Syntax

              .oO(Chung Leong)
              [color=blue]
              >Whenever I see people implement getter/setter functions in PHP, I don't know
              >if I should laugh or cry.[/color]

              There's nothing wrong with that.
              [color=blue]
              >Why on earth are you dragging that baggage from
              >the primitive wilderness of C++ into another language?[/color]

              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.
              [color=blue]
              >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.[/color]

              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

              Comment

              • Chung Leong

                #8
                Re: Use of Complex (Curly Bracket) Syntax

                "Michael Fesser" <netizen@gmx.ne t> wrote in message
                news:r8om21158l 1ik1o7k6fpbn69k 4plbbsmpf@4ax.c om...[color=blue]
                > There's nothing wrong with that.[/color]

                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.
                [color=blue]
                > 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.[/color]

                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.


                Comment

                • Jerry Stuckle

                  #9
                  Re: Use of Complex (Curly Bracket) Syntax

                  Chung Leong wrote:[color=blue]
                  >
                  > "Michael Fesser" <netizen@gmx.ne t> wrote in message
                  > news:r8om21158l 1ik1o7k6fpbn69k 4plbbsmpf@4ax.c om...[color=green]
                  > > There's nothing wrong with that.[/color]
                  >
                  > 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.
                  >[color=green]
                  > > 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.[/color]
                  >
                  > 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.[/color]


                  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@attgl obal.net
                  Member of Independent Computer Consultants Association - www.icca.org

                  Comment

                  Working...