All,

I'm trying to create a simple class mapping by having a static array that maps our current class members a different "name".

Is it possible to do something like

public function __get($member)
{
     if (isset($this->Mapping[$member]))
          return $this->eval(Mapping[$Member]);
}

of even simpler

public function __get($member)
{
     if (isset($this->$member))
          return $this->$member;
}

Most of the __get samples I've seen just use an array to hold data, but I'm trying to access actual public members of a class.

Thanks
-Dave

    well after a bit of personal investigation I have come to the conclusion that this:

    public function __get($member)
    {
        if (isset(self::$Mapping[$member]))
        {
            $tempMember = self::$Mapping[$member];
            return $this->$tempMember;
        }
    }
    

    works but this:

    public function __get($member)
    {
        if (isset(self::$Mapping[$member]))
        {
            return $this->self::$Mapping[$member];
        }
    }
    

    results in a parse error so you just have to save your mapped member to a variable before trying to access it.

    -Dave

      Sorry for talking to my self here 😉

      but one last thing. Overriding the get means all private properties are now accessible so I added some checks to better protect data:

      public function __get($member)
      {
          if (property_exists(self, $member))
              return $this->$member;
      
      if (isset(self::$Mapping[$member]))
      {
          $tempMember = self::$Mapping[$member];
          if (property_exists(self, $tempMember))
              return $this->$tempMember;
      }
      
      print "$this->Type tried to access non-existent/private property: $member";
      }
      

      I would appreciate any feedback on this since I just created it kinda on the fly right now.

      Thanks,
      -Dave

        Write a Reply...