3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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);
Output for 7.0.0 - 7.1.0
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; CParent has a deprecated constructor in /in/UrVsM on line 4 CChild Object ( [arrMember:protected] => Array ( [0] => v1 [1] => v2 [2] => v3 ) [mark:protected] => v1 )
Output for 5.0.0 - 5.6.28
CChild Object ( [arrMember:protected] => Array ( [0] => v1 [1] => v2 [2] => v3 ) [mark:protected] => v1 )
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/UrVsM on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/UrVsM on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/UrVsM on line 6
Process exited with code 255.