以下のように、抽象クラスでプロパティ名を指定して取得する方法はありませんでしょうか?
よろしくおねがいします。
lang
1 2abstract class parentClass 3{ 4 public function getProperty($name) 5 { 6 return $this->$name; // ここの記述がわからない 7 } 8} 9 10class childClass1 extends parentClass 11{ 12 private $name1; 13 private $description1; 14} 15 16class childClass2 extends parentClass 17{ 18 private $name2; 19 private $description2; 20} 21 22$test1 = new childClass1(); 23echo $test1->getProperty('name1'); // childClass1の$name1の値を取得したい 24 25$test2 = new childClass2(); 26echo $test2->getProperty('name2'); // childClass2の$name2の値を取得したい 27
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。