从类创建对象返回另一个类的实例

时间:2013-03-27 11:43:10

标签: php oop

我有类DatabaseAccess,我继承了两个类:TeamAccess和StatisticsAccess。现在,当我从StatisticsAccess创建TeamAccess实例时,它的类型是StatisticsAccess吗?这里发生了什么?

继承代码

class StatisticsAccess extends DatabaseAccess {
  public function getMatchesById($matchesid) {
    $teamAccess = new TeamAccess();
    print_r(new TeamAccess);
  }
}

class TeamAccess extends DatabaseAccess {
  function __construct() {
    parent::__construct();
  }
}

print_r显示StatisticsAccess对象。

1 个答案:

答案 0 :(得分:0)

在StatisticAccess中创建TeamAccess类只会创建一个TeamAccess类,就像在类之外创建它一样。

class StatisticsAccess extends DatabaseAccess {
    public function test() {
        $teamAccess = new TeamAccess();
        //print_r(new TeamAccess);
        echo "teamAccess var class: " . get_class($teamAccess);
        echo "<br/>this class: " . get_class($this);
    }
}

$sa = new StatisticsAccess;
$sa->test()
?>

返回:

//teamAccess var class: TeamAccess
//this class: StatisticsAccess