Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class A
- {
- public function printVars()
- {
- $r = ' // get_called_class() == '.get_called_class().
- ' // __CLASS__ == '.__CLASS__.
- ' // get_object_vars($this):';
- foreach(get_object_vars($this) as $key => $val){
- $r .= ' ->'.$key.' == "'.$val.'"';
- }
- return $r;
- }
- }
- class B extends A
- {
- public function printVars() // actually overloading afaik
- {
- $r = ' // get_called_class() == '.get_called_class().
- ' // __CLASS__ == '.__CLASS__.
- ' // get_object_vars($this):';
- foreach(get_object_vars($this) as $key => $val){
- $r .= ' ->'.$key.' == "'.$val.'"';
- }
- return $r;
- }
- }
- class C extends B
- {
- }
- echo '<pre>';
- $a = new A;
- $a->that = 'that for A';
- $b = new B;
- $b->that = 'that for B';
- $c = new C;
- echo '
- $a = new A;
- $a->that = \'that for A\';
- $b = new B;
- $b->that = \'that for B\';
- $c = new C;
- $a->printVars();'.$a->printVars().'
- $b->printVars();'.$b->printVars().'
- $c->printVars();'.$c->printVars().'
- ';
- $c->that = 'that for C';
- echo '
- $c->that = \'that for C\';
- $a->printVars();'.$a->printVars().'
- $b->printVars();'.$b->printVars().'
- $c->printVars();'.$c->printVars().'
- ';
- echo '<hr>
- using __set()';
- class A2
- {
- // private $that;
- // protected $that;
- public function __set($name,$value)
- {
- $this->$name = $value;
- }
- public function printVars()
- {
- $r = ' // get_called_class() == '.get_called_class().
- ' // __CLASS__ == '.__CLASS__.
- ' // get_object_vars($this):';
- foreach(get_object_vars($this) as $key => $val){
- $r .= ' ->'.$key.' == "'.$val.'"';
- }
- return $r;
- }
- }
- class B2 extends A2
- {
- // private $that;
- // protected $that;
- public function __set($name,$value)
- {
- $this->$name = $value;
- }
- public function printVars() // actually overloading afaik
- {
- $r = ' // get_called_class() == '.get_called_class().
- ' // __CLASS__ == '.__CLASS__.
- ' // get_object_vars($this):';
- foreach(get_object_vars($this) as $key => $val){
- $r .= ' ->'.$key.' == "'.$val.'"';
- }
- return $r;
- }
- }
- class C2 extends B2
- {
- // private $that;
- // protected $that;
- public function __set($name,$value)
- {
- $this->$name = $value;
- }
- }
- echo '<pre>';
- $a = new A2;
- $a->that = 'that for A2';
- $b = new B2;
- $b->that = 'that for B2';
- $c = new C2;
- echo '
- $a = new A2;
- $a->that = \'that for A2\';
- $b = new B2;
- $b->that = \'that for B2\';
- $c = new C2;
- $a->printVars();'.$a->printVars().'
- $b->printVars();'.$b->printVars().'
- $c->printVars();'.$c->printVars().'
- ';
- $c->that = 'that for C2';
- echo '
- $c->that = \'that for C2\';
- $a->printVars();'.$a->printVars().'
- $b->printVars();'.$b->printVars().'
- $c->printVars();'.$c->printVars().'
- ';
- ?>
- output:
- $a = new A;
- $a->that = 'that for A';
- $b = new B;
- $b->that = 'that for B';
- $c = new C;
- $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
- $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
- $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this):
- $c->that = 'that for C';
- $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
- $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
- $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this): ->that == "that for C"
- using __set()
- $a = new A2;
- $a->that = 'that for A2';
- $b = new B2;
- $b->that = 'that for B2';
- $c = new C2;
- $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
- $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
- $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this):
- $c->that = 'that for C2';
- $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
- $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
- $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for C2"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement