<?php
abstract class Universe
{
protected $PropertyNames = '["id"]';
static $_names;
/*
From the PHP documentation:
Note:
PHP implements a superset of JSON - it will also encode and decode scalar
types and NULL. The JSON standard only supports these values when they are
nested inside an array or an object.
Here we are trying to utilize this "superset decoding" to simplify the code.
In case it may fail in the future, the generic exception is stuffed behind a
decoding result check.
(In case you fail the inheritance, you're the one to be blamed.)
*/
public function __construct()
{
var_dump($this->PropertyNames);
if(!is_array(self::$_names))
{
if(!is_array($names = json_decode($this->PropertyNames)))
throw new Exception('PHP/JSON "superset decoding" failed.');
self::$_names = array_map('ucfirst', $names);
}
}
public function __call($method, $arguments)
{
return print_r(self::$_names, true);
/*
if(substr_compare($method, 'get', 0, 3) === 0)
{
$property = substr($method, 3);
if(in_array($property, $this->_names))
return $this->properties[$property];
}
else if(substr_compare($method, 'set', 0, 3) === 0)
{
$property = substr($method, 3);
if(in_array($property, $this->_names))
{
$this->properties[$property] = $arguments[0];
return $this;
}
}
throw new BadMethodCallException('Our magic was not strong enough to make it happen. But still, we have tried!');
*/
}
}
class Galaxy extends Universe
{
protected $PropertyNames = '["id","name"]';
static $_names;
public function __construct($id, $name)
{
parent::__construct();
// $this->setId($id);
// $this->setName($name);
}
}
class Starsystem extends Galaxy
{
protected $PropertyNames = '["id","name","coords","parent"]';
static $_names;
}
$g = new Galaxy(1, 'Galaxy');
$s = new Starsystem(2, 'Starsystem');
var_dump($s->x());