301
Web Programming and Design / Re: Your favorite web design tools/tips/tricks/tutorials/sites
« on: November 10, 2015, 12:31:27 pm »
technically tricks fit in here, too
So yeah, just did this thing:
So yeah, just did this thing:
Code: [Select]
<?php
class Foo {
protected $hello = 'there';
public function __construct(){
$this->hello = 'world';
}
public function upgrade(){
$reflect = new ReflectionClass($this);
$props = array();
foreach($reflect->getProperties() as $p){
$name = $p->name;
$props[$name] = &$this->$name;
}
return new Bar($props);
}
}
class Bar extends Foo {
public function __construct($props){
foreach($props as $var => $val){
$this->$var = $val;
}
}
public function test(){
echo $this->hello;
}
}
$obj = new Foo();
var_dump($obj);
$obj = $obj->upgrade();
var_dump($obj);
$obj->test();
?>
Allows you to "upgrade" a class to another one and copy all the public/protected vars over to the new one.