PHP Features
<?php | |
// Late Static Binding in PHP are great. | |
// However they are not supported in every version of PHP | |
// Use the code below in a new 3val.org window to test support | |
// for Late Static Bindings inside closures. | |
abstract class Demo | |
{ | |
protected $item; | |
protected $calledClass; | |
public function __construct($bean) | |
{ | |
if( !empty($bean) ){ | |
$this->item = $bean; | |
} | |
$this->calledClass = get_called_class(); | |
// Some other code here | |
} | |
public function getCalledClass() | |
{ | |
return $this->calledClass; | |
} | |
public static function parseArray($items) | |
{ | |
return array_map(function($item){ | |
return new static($item); | |
}, $items); | |
} | |
} | |
final class Proto extends Demo | |
{ | |
} | |
$list = [1,2,3,4]; | |
print_r( array_map(function($i){ | |
if( $i instanceof \Demo || $i instanceof \Proto ){ | |
return $i->getCalledClass(); | |
} | |
return 'Is null'; | |
}, Proto::parseArray($list) ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment