Variable value as object handler?

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I'm having some trouble with this in another project and my former solution is no longer working so once again I have to visit this question. Is there a way to assign a variable a value, and then for that variable to act as the object with the same name of the value it was given?

For example, lets say I had this code:
Code: [ Select ]
$title = 'HOME';
$title = strtolower($title).'_HL';
  1. $title = 'HOME';
  2. $title = strtolower($title).'_HL';


Would there be a way for me to get $title to act as a handler for an object $home_HL so that I could call $home_HL->get_name() and it would return $home_HL's name? My previous solution was to use globals like so:
Code: [ Select ]
$GLOBALS[$title]->get_name(); //Would return the name of the object.

However, for my project, register_globals has been disabled and there isn't a good way to get them turned back on so I am at a loss as to what I can do. Any help would be greatly appreciated!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • nexuslite
  • Newbie
  • Newbie
  • nexuslite
  • Posts: 10
  • Loc: Keizer, OR

Post 3+ Months Ago

I'm not completely sure what this is about but this should solve any global scope problem.

Code: [ Select ]
class Title {
    var $name = 'HOME';
    function get_name() {
        return $this->name;
    }
}

function Test() {
    $title = new Title;
    echo $title->get_name();
}

Test();
  1. class Title {
  2.     var $name = 'HOME';
  3.     function get_name() {
  4.         return $this->name;
  5.     }
  6. }
  7. function Test() {
  8.     $title = new Title;
  9.     echo $title->get_name();
  10. }
  11. Test();
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Try adding another $ in front of the variable holding the name of the object.

Code: [ Select ]
$obj = 'home_HL';
$$obj->method();
  1. $obj = 'home_HL';
  2. $$obj->method();

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 118 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.