0

私は3つのクラスを持っています..

クラス 1 :

<?php
include "two.php";
include "three.php";
class One{
    public function __construct(){
        $two = new Two($this);
        $three = new Three($two);
    }
}
$api = new One;
?>

クラス 2 :

<?php
class Two extends AOP {
    public function __construct($obj){
        //blablabla
    }
}
?>

クラス 3 :

<?php
class Three extends AOP {
    public function __construct($obj){
        echo get_class($obj);
    }
}
?>

しかし、結果は「One」を出力する必要があります。オブジェクト内のオブジェクトからクラス名を取得するには?

4

2 に答える 2

0

キーワードextendsを使用して、別のクラスを継承します。PHP は多重継承を直接サポートしていないためです。parent::$property;またはで拡張元のクラスを取得できますparent::method();。したがって、おそらくコードをより似たものにしたいと思うでしょう。

// three.php
class Three extends AOP{
  public function __construct($obj){
    echo get_class($obj);
  }
}

// two.php
class Two extends Three{
  public function __construct($obj){
    parent::__construct($obj); // Constructors do not return a value echo works
  }
  protected function whatever($string){
    return $string;
  }
}

// one.php
include 'three.php'; // must be included first for Two to extend from
include 'two.php'
class One extends Two{
  public function __construct(){
    // change this part
    parent::__construct($this); // uses the parent Constructor
    echo $this->whatever('. Is this what you mean?'); // call method without same name in this class - from parent
  }
}
$api = new One;

私はあなたの構造をまったく使用しませんが、これにより継承のアイデアが得られるはずです。

于 2013-09-22T21:24:59.070 に答える