SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can we use a variable to call object property or method

    Hi there,

    I have a simple PHP question.

    Assuming PHP5,

    We know this is possible:
    PHP Code:
    $var 'someName';

    echo 
    $theArray[$var]; 
    But we can't do this with an object:
    PHP Code:
    echo $theObject->$var
    Is there a way to do this?

    Thanks in advance.


    That was the wrong question, see my third post for the right question.
    Last edited by twenty205; May 16, 2007 at 02:41. Reason: Mistake in the question

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $theObject->${$var}; 
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much That's great!

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about inside a method when assigning, any way to do it in there?
    PHP Code:
    class = myClass {

        public 
    $var;

        function 
    __construct($newVal,$key) {

            
    $this->$($key) = $newVal;

        }

    }

    $theClass = new myClass('someValue','var'); 
    It doesn't seem to work.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by twenty205 View Post
    But we can't do this with an object:
    PHP Code:
    echo $theObject->$var
    Actually, you can.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    PHP Code:
    echo $theObject->${$var}; 
    Er... wouldn't that access a class variable whose name is stored in a variable whose name is in $var? Something like...
    PHP Code:
    $var 'foo';
    $theObject->${$var}; //equivalent to accessing $theObject->{$foo}
    $theObject->{$var};  //equivalent to accessing $theObject->foo 
    In fact, one can call methods with the nomenclature $obj->$var_name but the braces seem to be favoured these days, and would resolve any ambiguous cases.
    Zealotry is contingent upon 100 posts and addiction 200?

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Actually, you can.
    Yeah I made a mistake, asked the wrong question, assuming it was the same question.

    Do you know if there is any way to do it in a method/constructor as above?

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by twenty205 View Post
    Do you know if there is any way to do it in a method/constructor as above?
    You mean:
    PHP Code:
    function __construct($newVal$key) {
      
    $this->$key $newVal;

    ?

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah thanks, could've sworn I already tried that, must've had another error somewhere, thanks everyone.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •