OO Magic

    Date: 07/04/06 (PHP Development)    Keywords: no keywords

    I did a bit of research today, and came up with the following interesting method:

    class MyClass {
      private $sub_data; // some random object
    
      public function doSomething() {
        ...
      }
    
      private function __call($name, $args) {
        if( is_object($this->sub_data) ) {
          return call_user_func(array(&$this->sub_data, $name), $args);
        }
        else {
          echo "Error: sub_data is not an object.  This method was not found\n";
          return;
        }
      }
    }
    


    I have a class defined with a property ($this->sub_data) that holds a variable class instance. What the $__call() method allows me to do is to proxy method calls invoked on my "container" class down to the $this->sub_data class without having to know anything about the sub_data object instance.

    More info:
    Overloading
    call_user_func

    Source: http://community.livejournal.com/php_dev/70642.html

« Maintaining Drop Down Menu... || resuming http file transfer »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home