I've seen some examples of similar things, but I can't find anything related to what I'm trying to do. Consider the case where you've got some large amount of mysql tables, and you want a function like 'myObject.tableName()' to get and print the information for that table.

Simplified a lot, I'd like to be able to do something akin to the following Javascript (sort of) code:

class SomeObject {
  function SomeObject() {
    this['nonExistantProperty'] = staticFunc;
    this['nonExistantProperty']();
  }

  function staticFunc() {
    trace( "Hello World" );
  }
}

So here's the PHP code so far:

class SomeObject {
  function SomeObject() {
    $table = 'myTable';
    $this->$table = &$this->staticFunc;
    $this->$table();
  }
  function staticFunc() {
    print( "Hello World" );
  }
}

Obviously I would enclose the creation of the dynamic member function in a foreach loop for each table, etc. etc.

Does anyone have any ideas? I've tried a few different examples I've found, but does this just not work in PHP? Does $this->staticFunc not return a pointer to the function, and is there anyway to get a reference to staticFunc to assign it to another variable?

    This is one way you could use:

    class SomeObject {
      private $methods = array();
    
      function SomeObject() {
        $this->methods['myTable'] = 'staticFunc';
        $this->{$this->methods['myTable']}();
      }
    
      function staticFunc() {
        print( "Hello World" );
      }
    }

      Another approach:

      class SomeObject {
        private $methods = array();
      
        function __construct()
        {
          $this->methods['myTable'] = array('self', 'staticFunc');
          $this->myTable();
        }
      
        function __call($method, $arguments)
        {
          if(isset($this->methods[$method]))
              call_user_func_array($this->methods[$method], $arguments);
        }
      
        static function staticFunc() { echo "Hello, World"; }
      }
      

      Another one, more like JavaScript and available from PHP 5.3 onwards:

      class SomeObject
      {
      	private $methods = array();
      
      public function __construct()
      {
      	$this->methods['myTable'] = function()
      	{
      		echo 'Hello, World';
      	};
          $this->myTable();
      }
      
        function __call($method, $arguments)
        {
          if(isset($this->methods[$method]))
              call_user_func_array($this->methods[$method], $arguments);
        }
      }
      
        Write a Reply...