Skip to content

Instantly share code, notes, and snippets.

@Page-Carbajal
Last active April 6, 2016 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Page-Carbajal/b4ade22d0adce5091d16ff091c3f8148 to your computer and use it in GitHub Desktop.
Save Page-Carbajal/b4ade22d0adce5091d16ff091c3f8148 to your computer and use it in GitHub Desktop.
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