Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Question variable variables and class property assignment

    i have a class:
    PHP Code:
    class tableClass {
       public 
    $testvar;

    the function of the full class is to assemble a table. I have .inc files for each table that must set multiple public properties of this table class. however, i want to keep the .inc files for each table uniform. like
    PHP Code:
    $table->testvar hello
    this presents a problem, because it requires that each class instance is called "$table". i thought i could solve the issue by using variable variables and leave the inc files as is, but use
    PHP Code:
    $myTable1 = new tableClass();
    $
    $table $myTable1
    and change the inc files to use $$table everywhere without hardcoding the instance names
    PHP Code:
    $$table->testvar 'hello'

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I've never used a variable variable with an object before, but it may be a similar problem to how the variable variables are treated when as an array, its not sure if you are looking at $table->method, or if you want $$table->method. So, the fix around for that is to use ${$table}->method(). See if that works.
    The other plus side is that if you assign $table a new table object, everytime its constructed $table takes the new object, so you may not require the use of variable variables for tracking them - I find that this tends to be the case more often. Try the force on it first, see if that fixes your problem.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    well i came up with a better solution...

    all of my .inc files are now:
    PHP Code:
    $this->varName 'myVar'
    i added a "loadCfg" method to my class simply that 'include's the file, this updates all of the object instance's properties. ends up being much slicker.

    hope this helps others, dont want to get the noodle out for nothing
    Leon

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah I see, so all of these are actually in an object then eh? Yes, always use $this when referring an object reference of itself, unless you are wanting specific behaviour in an extend able method. I've been caught in an infinite call from parent to child and back on a savable class I created once. Easy get around though, simply use self:: calls instead if it may be extended and the child may call the same class it also defines. Err, yeah kinda confusing sounding, I'll give you an example if you need one though :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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