Last active
April 6, 2016 20:35
-
-
Save Page-Carbajal/b4ade22d0adce5091d16ff091c3f8148 to your computer and use it in GitHub Desktop.
PHP Features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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