3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CParent { protected $arrMember = array(); protected $mark; function CParent(){ /* * If parent constructor run first, init() method'll get blank array. */ $this->setAttr(); } protected function init(){ return array(); } protected function setAttr(){ $arr = $this->init(); foreach ($arr as $item){ array_push($this->arrMember, $item['name']); if (isset($item['mark']) && $item['mark'] == TRUE) $this->mark = $item['name']; } } } class CChild extends CParent { /* * I'd like to define variable within this method then pass to parent class. */ protected function init(){ return array( array('name' => 'v1', 'mark' => true), array('name' => 'v2'), array('name' => 'v3') ); } } $c = new CChild(); print_r($c);

preferences:
28.82 ms | 402 KiB | 5 Q