How to call class A method inside class B method and to make class B methods and parameters available from class A method?
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 exec开发者_C百科ute($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?
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.
精彩评论