php - Transform one class to another class -
i want scheme of class versioning allow 1 scheme talk scheme in way future changes wouldn't impact what's in place.
let's scheme , scheme b talk each other via rpc calls. business requirements alter , scheme , b need changed future development while beingness backward compatible
the next work:
class base_version { public static function getversion($version = 1) { $versionclass = 'version_'.$version; homecoming new $versionclass(); } } class version_1 { public function sayhello() { echo "hello version 1\n"; } } class version_2 { public function sayhello() { echo "hello version 2\n"; } } $obj = base_version::getversion(); $obj->sayhello(); $obj = base_version::getversion(2); $obj->sayhello();
i don't static instancing however. this, except know can't reassign $this
.
class base_version { public function __construct($version) { $versionclass = 'version_'.$version; $this = new $versionclass(); } } $obj = new base_version(); $obj->sayhello(); $obj = new base_version(2); $obj->sayhello();
how can accomplish this?
i think abstract class defined constructor can close that.
let's remind abstract class need have @ to the lowest degree 1 abstract function, can have 'concrete'[sic]* functions same each inheriting class. abstract classes can't instanciated, children.
here is:
abstract class base_version {function __construct($version) {$this->version = $version; switch ($this->version) {case 1: // build class 1 break; case 2: // build class 2 etc. break; default: // careless programmers won't define constructor in future break;}} function version() {return $this->version;} abstract function sayhello();}
this base of operations abstract class. if business requirements change, can add together cases here new classes in constructor.
class greeter extends base_version {function sayhello() {return 'this old version' . $this->version(); }} class new_greeter extends base_version {function sayhello() {return 'hello version ' . $this->version(); }}
the point of abstract class here class not throw errors if extending abstract class, must implement function sayhello()
.
that way, whoever ever makes class extending base_version won't break programme because objects have @ to the lowest degree functions defined abstract in base of operations class.
so there iss, way define classes can alter in time without breaking way older ones work
$obj = new greeter(1); $obj ->sayhello(); $obj2 = new new_greeter(2); $obj2->sayhello();
'concrete' in c++ has specific meaning, don't want offend using here if it's not same php oop
No comments:
Post a Comment