Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created April 25, 2014 20:46
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 dongilbert/11302725 to your computer and use it in GitHub Desktop.
Save dongilbert/11302725 to your computer and use it in GitHub Desktop.
AbstractBaseEntity - the solution to the problem was to wrap the constructor `$this->entityData = $data` in an `if(empty($data))` block.
<?php
namespace Entities;
abstract class AbstractBaseEntity
{
protected $entityData = [];
final public function __construct($data = [])
{
if (is_object($data))
{
$data = get_object_vars($data);
}
$this->entityData = $data;
}
public function __get($name)
{
$method = $this->getMethodName($name);
if (method_exists($this, $method))
{
return $this->$method();
}
if (array_key_exists($name, $this->entityData))
{
return $this->entityData[$name];
}
return null;
}
public function __set($name, $value)
{
$this->entityData[$name] = $value;
}
/**
* Transforms a property name into a method
* name. Swaps snake_case to camelCase
*
* @param $name
*/
private function getMethodName($name)
{
return 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment