We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Well, this is strictly a PHP question, but I know you guys are smart and friendly, so I’m hoping someone can explain this to me.

    I’m trying to store some data in a variable location in an object, and that data is various hash keys.

    Consider the following code contained in one of my object’s functions:

    $storage_loc = 'somewhere';
    
    //  $this->$storage_loc[$key] = $value;  // don't work
    //  $this->$storage_loc->$key = $value;  // works, but stores as an object
    //  $this->$storage_loc = array_merge( $this->$storage_loc, array($key => $value) );  // Doesn't work
      $this->vars[$key] = $value;		   // static properties work fine


    Why can’t I use a variable to identify an object PROPERTY? I can use a variable to define an object FUNCTION without any problems.

    Thanks for any tips.
      • 16183
      • 1,390 Posts
      Sorry to to ask a question of my own using your thread Everett. I have just resumed learning PHP (almost a total noob) and I am wondering whether

      $this->$storage_loc[$key] = $value;


      is correct syntax? My reading tells me that the -> symbol is used to connect objects to their properties or methods, and that the $ symbol is omitted before the variable/property name when accessing properties of a class instance. So, should be

      $this->storage_loc[$key] = $value;


      is that correct? Many thanks.

      Like I said, I have only just started so feel free to ignore me since I probably don’t know what am talking about wink

      cheers/k
        • 3749
        • 24,544 Posts
        I not sure exactly what you’re trying to do, but I think this might be what you want (and Kongondo is correct about the extra dollar sign):

        $storage_loc = array();
        
        $this->storage_loc[$key] = $value;  

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 5274
          • 177 Posts
          Sounds like you want to use traditional set and get methods (functions) to use a object (class) as a Data Transfer Object or what used to be call a Value Object.

          // ==================================================
          // START function
          
          function setName($name) {
          
              $this -> name = $name;
          
          } // END function
          // ==================================================
          
          // ==================================================
          // START function
          
          function getName() {
          
              return $this -> name;
          
          } // END function
          // ==================================================
          


          This type of code is commonly used in conjunction with Data Access Objects.
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            No no, I am trying to set variable properties, so the syntax really is $this->$x

            This works fine for a regular scalar variable. E.g. I can iterate over an array( ’a’,’b’,’c’) then perform a function on those, e.g.
            $this->$x = $_POST[$x];

            But what doesn’t work is storing the hashes. At least not directly. You can do something like $this->$x = array($param => $result);

            This is different than the more common static methods (i.e. $this->x = array($param => $result); )


              • 28042 ☆ A M B ☆
              • 24,524 Posts
              So this is related to "variable variables", which is something that always fascinated me, but I never really did get my head wrapped around.
              http://us2.php.net/manual/en/language.variables.variable.php

                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 5274
                • 177 Posts
                Sorry for the misunderstanding. I wonder if this a job for enclosing the variable in question with curly brackets?
                  • 3749
                  • 24,544 Posts
                  Sorry, I thought you were attempting something simpler. I’ve never found a need for that form (always found a way to use straight associative arrays) and I’ve suspected that it would make my code harder to maintain and debug so, like Susan, I’m not much help on this one. I suspect that Chunk is correct about the curly braces.

                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    Yeah, I tried the variable variables syntax, but couldn’t get it to work. Ultimately, I solved the issue in another way that cleaned up my code smiley

                    I guess the lesson is that if it takes too long to come up with some crazy syntax, then maybe there’s an easier way...
                      • 16183
                      • 1,390 Posts
                      ...And I have learned something new... smiley

                      cheers/k