Variable Variables in Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Little

    Variable Variables in Classes

    class example {
    var $Stage;

    function example() {
    $this->Stage = 'init';

    $x = ???????;

    echo $$x;
    }
    }

    What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
    nor does 'this->Stage'.

    --
    Alan Little
    Phorm PHP Form Processor

  • Ambush Commander

    #2
    Re: Variable Variables in Classes

    It looks like in this particular case, $this->$x should be sufficient.
    Otherwise, you may have to split up x into $x_object and $x_name
    (exploding by -> should be sufficient) and calling $x_object->$x_name.
    What you currently have, however, cannot be done.

    Comment

    • Rik

      #3
      Re: Variable Variables in Classes

      Alan Little wrote:[color=blue]
      > class example {
      > var $Stage;
      >
      > function example() {
      > $this->Stage = 'init';
      >
      > $x = ???????;
      >
      > echo $$x;
      > }
      > }
      >
      > What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
      > nor does 'this->Stage'.[/color]


      Why use the $$ syntax?

      <?php

      class test{
      var $string;
      function text(){
      $this->string = "This works";
      $check = 'string';
      echo $this->$check;
      }
      }
      $a = new test();
      $a->text();
      ?>

      Grtz,
      --
      Rik Wasmus


      Comment

      • Alan Little

        #4
        Re: Variable Variables in Classes

        Carved in mystic runes upon the very living rock, the last words of
        Ambush Commander of comp.lang.php make plain:
        [color=blue]
        > It looks like in this particular case, $this->$x should be sufficient.[/color]

        Thanks, but no.
        [color=blue]
        > Otherwise, you may have to split up x into $x_object and $x_name
        > (exploding by -> should be sufficient) and calling $x_object->$x_name.
        > What you currently have, however, cannot be done.[/color]

        I'm not sure what you mean. What I'm trying to do is parse variables out
        of a string and substitute them with the value. $x (in this example)
        would hold the variable name extracted from the string. I'm trying to
        figure out how to get to the value of that variable, from there.

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Rik

          #5
          Re: Variable Variables in Classes

          Alan Little wrote:[color=blue]
          > Carved in mystic runes upon the very living rock, the last words of
          > Ambush Commander of comp.lang.php make plain:
          >[color=green]
          >> It looks like in this particular case, $this->$x should be
          >> sufficient.[/color]
          >
          > Thanks, but no.
          >[color=green]
          >> Otherwise, you may have to split up x into $x_object and $x_name
          >> (exploding by -> should be sufficient) and calling
          >> $x_object->$x_name. What you currently have, however, cannot be done.[/color]
          >
          > I'm not sure what you mean.
          > What I'm trying to do is parse variables
          > out of a string and substitute them with the value. $x (in this
          > example) would hold the variable name extracted from the string. I'm
          > trying to figure out how to get to the value of that variable, from
          > there.[/color]

          That is exactly what it does.
          $x = 'string'

          $$x = $this->$x;
          equals:
          $string = $this->string;

          $x is the variable name, $variable_name holds the value......

          I don't get the problem?

          What is the exact reason you're trying to accomplish? Maybe if you elaborate
          some more, we get the picture. Why is it this doesn't suffice?

          Grtz,
          --
          Rik Wasmus


          Comment

          • Alan Little

            #6
            Re: Variable Variables in Classes

            Carved in mystic runes upon the very living rock, the last words of Rik
            of comp.lang.php make plain:
            [color=blue]
            > Alan Little wrote:[color=green]
            >> Carved in mystic runes upon the very living rock, the last words of
            >> Ambush Commander of comp.lang.php make plain:
            >>[color=darkred]
            >>> It looks like in this particular case, $this->$x should be
            >>> sufficient.[/color]
            >>
            >> Thanks, but no.
            >>[color=darkred]
            >>> Otherwise, you may have to split up x into $x_object and $x_name
            >>> (exploding by -> should be sufficient) and calling
            >>> $x_object->$x_name. What you currently have, however, cannot be
            >>> done.[/color]
            >>
            >> I'm not sure what you mean.
            >> What I'm trying to do is parse variables
            >> out of a string and substitute them with the value. $x (in this
            >> example) would hold the variable name extracted from the string. I'm
            >> trying to figure out how to get to the value of that variable, from
            >> there.[/color]
            >
            > That is exactly what it does.
            > $x = 'string'
            >
            > $$x = $this->$x;
            > equals:
            > $string = $this->string;
            >
            > $x is the variable name, $variable_name holds the value......
            >
            > I don't get the problem?
            >
            > What is the exact reason you're trying to accomplish? Maybe if you
            > elaborate some more, we get the picture. Why is it this doesn't
            > suffice?[/color]

            For example:

            $this->animal = 'lamb';
            $this->string = 'Mary had a little {{animal}}';

            The goal is to extract 'animal' from the string (no problem), convert it
            to 'lamb' and replace it in the string (again, no problem). The question
            is, if I have 'animal', how do I get 'lamb'? In global space, I would
            simply have 'animal' in a variable, and use $$var to get 'lamb'.

            --
            Alan Little
            Phorm PHP Form Processor

            Comment

            • Alan Little

              #7
              Re: Variable Variables in Classes

              Carved in mystic runes upon the very living rock, the last words of Rik
              of comp.lang.php make plain:
              [color=blue]
              > Alan Little wrote:[color=green]
              >> class example {
              >> var $Stage;
              >>
              >> function example() {
              >> $this->Stage = 'init';
              >>
              >> $x = ???????;
              >>
              >> echo $$x;
              >> }
              >> }
              >>
              >> What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
              >> nor does 'this->Stage'.[/color]
              >
              > Why use the $$ syntax?
              >
              > <?php
              >
              > class test{
              > var $string;
              > function text(){
              > $this->string = "This works";
              > $check = 'string';
              > echo $this->$check;[/color]

              Ah, yes that works. Thank you.

              --
              Alan Little
              Phorm PHP Form Processor

              Comment

              • Kimmo Laine

                #8
                Re: Variable Variables in Classes

                "Alan Little" <alan@n-o-s-p-a-m-phorm.com> wrote in message
                news:Xns97C6A5C 373D05alanphorm com@216.196.97. 131...[color=blue]
                > Carved in mystic runes upon the very living rock, the last words of Rik
                > of comp.lang.php make plain:
                >[color=green]
                >> Alan Little wrote:[color=darkred]
                >>> Carved in mystic runes upon the very living rock, the last words of
                >>> Ambush Commander of comp.lang.php make plain:
                >>>
                >>>> It looks like in this particular case, $this->$x should be
                >>>> sufficient.
                >>>
                >>> Thanks, but no.
                >>>
                >>>> Otherwise, you may have to split up x into $x_object and $x_name
                >>>> (exploding by -> should be sufficient) and calling
                >>>> $x_object->$x_name. What you currently have, however, cannot be
                >>>> done.
                >>>
                >>> I'm not sure what you mean.
                >>> What I'm trying to do is parse variables
                >>> out of a string and substitute them with the value. $x (in this
                >>> example) would hold the variable name extracted from the string. I'm
                >>> trying to figure out how to get to the value of that variable, from
                >>> there.[/color]
                >>
                >> That is exactly what it does.
                >> $x = 'string'
                >>
                >> $$x = $this->$x;
                >> equals:
                >> $string = $this->string;
                >>
                >> $x is the variable name, $variable_name holds the value......
                >>
                >> I don't get the problem?
                >>
                >> What is the exact reason you're trying to accomplish? Maybe if you
                >> elaborate some more, we get the picture. Why is it this doesn't
                >> suffice?[/color]
                >
                > For example:
                >
                > $this->animal = 'lamb';
                > $this->string = 'Mary had a little {{animal}}';
                >
                > The goal is to extract 'animal' from the string (no problem), convert it
                > to 'lamb' and replace it in the string (again, no problem). The question
                > is, if I have 'animal', how do I get 'lamb'? In global space, I would
                > simply have 'animal' in a variable, and use $$var to get 'lamb'.
                >[/color]


                So again, wouldn't this work:

                $var = 'animal';
                $this->$var; //this is translated to $this->animal, which in turn gives you
                'lamb' .


                --
                "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
                spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


                Comment

                • Alan Little

                  #9
                  Re: Variable Variables in Classes

                  Carved in mystic runes upon the very living rock, the last words of
                  Kimmo Laine of comp.lang.php make plain:
                  [color=blue]
                  > "Alan Little" <alan@n-o-s-p-a-m-phorm.com> wrote in message
                  > news:Xns97C6A5C 373D05alanphorm com@216.196.97. 131...[color=green]
                  >> Carved in mystic runes upon the very living rock, the last words of
                  >> Rik of comp.lang.php make plain:
                  >>[color=darkred]
                  >>> Alan Little wrote:
                  >>>> Carved in mystic runes upon the very living rock, the last words of
                  >>>> Ambush Commander of comp.lang.php make plain:
                  >>>>
                  >>>>> It looks like in this particular case, $this->$x should be
                  >>>>> sufficient.
                  >>>>
                  >>>> Thanks, but no.
                  >>>>
                  >>>>> Otherwise, you may have to split up x into $x_object and $x_name
                  >>>>> (exploding by -> should be sufficient) and calling
                  >>>>> $x_object->$x_name. What you currently have, however, cannot be
                  >>>>> done.
                  >>>>
                  >>>> I'm not sure what you mean.
                  >>>> What I'm trying to do is parse variables
                  >>>> out of a string and substitute them with the value. $x (in this
                  >>>> example) would hold the variable name extracted from the string.
                  >>>> I'm trying to figure out how to get to the value of that variable,
                  >>>> from there.
                  >>>
                  >>> That is exactly what it does.
                  >>> $x = 'string'
                  >>>
                  >>> $$x = $this->$x;
                  >>> equals:
                  >>> $string = $this->string;
                  >>>
                  >>> $x is the variable name, $variable_name holds the value......
                  >>>
                  >>> I don't get the problem?
                  >>>
                  >>> What is the exact reason you're trying to accomplish? Maybe if you
                  >>> elaborate some more, we get the picture. Why is it this doesn't
                  >>> suffice?[/color]
                  >>
                  >> For example:
                  >>
                  >> $this->animal = 'lamb';
                  >> $this->string = 'Mary had a little {{animal}}';
                  >>
                  >> The goal is to extract 'animal' from the string (no problem), convert
                  >> it to 'lamb' and replace it in the string (again, no problem). The
                  >> question is, if I have 'animal', how do I get 'lamb'? In global
                  >> space, I would simply have 'animal' in a variable, and use $$var to
                  >> get 'lamb'.[/color]
                  >
                  > So again, wouldn't this work:
                  >
                  > $var = 'animal';
                  > $this->$var; //this is translated to $this->animal, which in turn
                  > gives you 'lamb' .[/color]

                  Yes, that works; I just didn't understand what AB was saying.

                  --
                  Alan Little
                  Phorm PHP Form Processor

                  Comment

                  • Dikkie Dik

                    #10
                    Re: Variable Variables in Classes

                    > For example:[color=blue]
                    >
                    > $this->animal = 'lamb';
                    > $this->string = 'Mary had a little {{animal}}';
                    >
                    > The goal is to extract 'animal' from the string (no problem), convert it
                    > to 'lamb' and replace it in the string (again, no problem). The question
                    > is, if I have 'animal', how do I get 'lamb'? In global space, I would
                    > simply have 'animal' in a variable, and use $$var to get 'lamb'.
                    >[/color]
                    Ah. Why not use str_replace?

                    And when you are using classes, you could as easily define a Variable
                    class with a Name and a Value method, and even a ToTemplate method that
                    puts the two pairs of curly braces around them.

                    If this is for a template engine, you probably have the name/value pairs
                    in a "named" array. In that case, a str_replace in a foreach loop does
                    the job just fine.

                    Best regards

                    Comment

                    • Alan Little

                      #11
                      Re: Variable Variables in Classes

                      Carved in mystic runes upon the very living rock, the last words of
                      Dikkie Dik of comp.lang.php make plain:
                      [color=blue][color=green]
                      >> For example:
                      >>
                      >> $this->animal = 'lamb';
                      >> $this->string = 'Mary had a little {{animal}}';
                      >>
                      >> The goal is to extract 'animal' from the string (no problem), convert
                      >> it to 'lamb' and replace it in the string (again, no problem). The
                      >> question is, if I have 'animal', how do I get 'lamb'? In global
                      >> space, I would simply have 'animal' in a variable, and use $$var to
                      >> get 'lamb'.[/color]
                      >
                      > Ah. Why not use str_replace?[/color]

                      The replacing part is not a problem.
                      [color=blue]
                      > And when you are using classes, you could as easily define a Variable
                      > class with a Name and a Value method, and even a ToTemplate method
                      > that puts the two pairs of curly braces around them.[/color]

                      I'm not sure I follow you here. I'm trying to get value names *from* a
                      string, and then get the values for those names.
                      [color=blue]
                      > If this is for a template engine, you probably have the name/value
                      > pairs in a "named" array.[/color]

                      For some values, yes. But in other cases, such as error reporting, I need
                      to access an actual variable.

                      Anyway, I have it worked out now. But thank you for answering.

                      --
                      Alan Little
                      Phorm PHP Form Processor

                      Comment

                      Working...