OOP Question

ok, so what i trying to do might be against OOP rules and not possible, but please tell how i can do this:


class abc{

public $page;

public function abs123(){
.....
}

public function runpage(){
$this->page = 'abs123';
$this->page();	
		
	}
}


when calling $this->page returns error: Fatal error: Call to undefined method abs::page()

in the example above i was hoping that $this->page(); would become $this->abs123(); as I assigned page = ‘abs123’. is this possibel to do at all?

User call_user_fun to call a class method in run time.

class abc{ 

  public $page; 

  public function abs123(){ 
    print 'Humpty Dumpty sat on a wall!'; 
  } 

  public function runpage(){ 
    $this->page = 'abs123'; 
    call_user_func(array($this, $this->page));    
  } 
}

I actually don’t know, haha. But here’s another way to look at it…
I wouldn’t recommend it, but food for thought.


// This is how it's called:
new PageLoader('abs123');

class PageLoader
{
    // This happens everytime you do a new PageLoader(); 
    public function __construct($var)
    {
        // Set the page name
        $this->page = $var;

        // Jump to the handlePage
        $this->_handlePage();
    }
    
    private function _handlePage()
    {
        // Call a function based on $this->page set above
        switch ($this->page)
        {
            case 'abs123':
                $this->abs123();
                break;
                
            case 'something':
                $this->something();
                break;
                
            defeault 'home':
                $this->home();
                break;
        }
    }

    // private functions to do all your pages.

    private function abs123()
    {
        // do stuff
    }

    private function something()
    {
        // do stuff
    }
    
    private function home()
    {
        // do stuff
    }

} 


running that errors out with this:
Warning: call_user_func(Array) [function.call-user-func]: First argument is expected to be a valid callback in

ohhh sorry this actually works… my bad… let me do some more testing please…

great function this is: call_user_func(array($this, $this->page));

thanks

You would have had to do


$this->{$this->page}();

But, call_user_func and call_user_func_array are more flexible, and imo more readable.