SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem calling/setting dynamic variables

    Hi,

    The problem i'm having may be related to the version of PHP i'm using.

    I have an object
    PHP Code:
    foo {

        private 
    $var1 = array();
        private 
    $var2 = array();

        function 
    foobar($t $d){
            
    $this->$t[] = $d;
        }


    The problem is $this->$t[] = $d; It isn't adding it to the relevant array.
    EG. when $t = 'var2', $this->$t[] should add a value to the $var2 array. But it's not doing that unless I hardcode the varible name.
    All the variables that are to be used are hardcoded in the object, none of the variables are to be created on the fly.

    Doing this on another local machine (at home) works fine, but on my work machine, the setting of a variable using $this->$t[] does not work.

    Is there an extension I could be missing?
    Or... am i being a total idiot?

    Cheers.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the following function
    PHP Code:
    public function foobar($t $d)

            if (isset(
    $this->$t))
                    
    array_push($this->$t$d); 


  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.
    I should have added, I also need to be able to set the keys so the array_push method wont work, auto inc keys are useless for the app.

    Cheers

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this

    PHP Code:
    public function foobar($t $d)
    {
            if (!isset(
    $this->$t))
                    
    $this->$t = array();
            
    array_push($this->$t$d);

    or this

    PHP Code:
    private $vars = array();
    public function 
    foobar($t$d)
    {
            if (!
    array_key_exists($t$this->vars))
                    
    $this->vars[$t] = array();
            
    array_push($this->vars[$t], $d);


  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Time out :P

    You're trying to pass $t as the name of the variable?
    PHP Code:
    foo 

        private 
    $var1 = array(); 
        private 
    $var2 = array(); 

        function 
    foobar($t $d){ 
            
    $this->$$t[] = $d
        } 


    See if you can spot the subtle change....
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This throws fatal error. There is an explanation and a workaround here (last post).


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
  •