tags:

views:

30

answers:

1

Hello,

class User
{

  private $_var_1 = 10;
  private $_var_2 = 20;

  private function _preExecute() {
    //do something here before executing sub-class's method
  }

  private function _postExecute() {
    //do something here after executing sub-class's method
  }

  private function _anyMethod() {
    echo "Hello!";
  }

  public function execute($action) {
    $this->_preExecute();
    $obj = new __CLASS__."_".$action;
    $obj->execute();
    $this->_postExecute();
  }

}

class User_Add 
{

  public function execute() {
    echo $this->_var1;
    echo $this->_var2;
    parent::_anyMethod();
  }

}

$user = new User();
$user->execute("Add");

So, as you can see I want to be able to access User class's variables and methods from class User_Add, is it possible in PHP 5.2 or higher?

Thank you.

A: 

Sure!

class User_Add extends User

But your functions and variables in the User class have to be protected or public to be accessible for the User_Add class. Take a look at inheritance in the php documentation.

Leon