Retrieving the name of the variable that holds the instance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aktar
    New Member
    • Jul 2006
    • 105

    Retrieving the name of the variable that holds the instance

    Consider the following codes:

    [PHP]class x
    {

    function get_instance_va r_name(){

    #HOW DO I GET THE NAME OF THE CURRENT INSTANCE???

    return($instanc e_name)

    }

    }

    $santas_red_und erpants = new x;
    print $santas_red_und erpants -> get_instance_va r_name();
    [/PHP]

    How do I get get_instance_na me() to find the name of the current instance, which is santas_red_unde rpants
  • satas
    New Member
    • Nov 2007
    • 82

    #2
    It is impossible in the way you have describe it.
    You should pass instance name manually, something like that :
    [PHP]class x {
    private $instanceName;
    public function __construct($in stanceName) {
    this->instanceName = $instanceName;
    }
    }

    $santas_red_und erpants = new x('santas_red_u nderpants');[/PHP]

    Anyway, why do you need to know instance name? Explain your problem. Maybe together we could find better solution.

    Regards.

    Comment

    • aktar
      New Member
      • Jul 2006
      • 105

      #3
      Great suggestion and very efficient too.

      I am trying to create a core of a framework that uses a single instances of the required classes for all tasks. But the risidual data creates a problem for any new tasks and results in unwanted effects.

      So now I'm thinking of storing unique data on a per-instance basis, while still using single instances of the required classes. That way I can keep the overheads to a minimum.

      Also look at the following for retrieving variable that holds the instance. Its extremely resource hungry, especially if a lot of calls need to be made but its a start:

      [PHP]
      function instance_name() {

      $instance = md5(serialize($ this));

      foreach($GLOBAL S as $variable => $value){

      if( md5(serialize($ value)) == $instance ){

      return $variable;
      }
      return(false);
      }

      }
      [/PHP]

      Comment

      • satas
        New Member
        • Nov 2007
        • 82

        #4
        Still haven't clear understanding of existing problem. Maybe you should use static objects if you want to have only one instance of class?
        Are you sure your solution isn't reinventing of wheel?

        Comment

        Working...