Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created October 30, 2012 21:08
Show Gist options
  • Select an option

  • Save jehoshua02/3983070 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/3983070 to your computer and use it in GitHub Desktop.
Calling method and class held in array
<?php
class FactoryClass
{
public static function build()
{
return new ProductClass();
}
}
class ProductClass
{
}
class ModelClass
{
protected $product;
public function modelMethod($product)
{
$this->product = $product;
}
}
$map = array(
'factory' => 'FactoryClass',
'callback' => 'modelMethod',
);
$model = new ModelClass();
$model->$map['callback']($map['factory']::build());
var_dump($model);
@jehoshua02
Copy link
Author

I did not think that $model->$map['callback']($map['factory']::build()); would work at all, but it did!

@jehoshua02
Copy link
Author

$ php --version
PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:19:58) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment