Last active
December 10, 2015 10:28
-
-
Save cpriest/4420904 to your computer and use it in GitHub Desktop.
Alternative to parent::$foo, rather than using that syntax, since accessors shadow properties, we could use $this->foo which would either access the property directly or call the parent foo accessor until there was no parent accessor which $this->foo would then access the property directly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
public $foo { | |
get { return $this->foo ?: 'unknown'; } | |
set { $this->foo = $value; } | |
} | |
} | |
class B extends A { | |
public $foo { | |
get { return $this->foo ?: 'unknown'; } | |
} | |
} | |
class C extends B { | |
public $foo { | |
get { return $this->foo ?: 'unknown'; } | |
} | |
} | |
$o = new C(); | |
$o->foo = 1; | |
echo $o->foo; /* echos 1 --> Calls C::__getfoo() | |
-> Calls B::__getFoo() | |
-> Calls C::__getFoo() which directly accesses $this->foo, previously set to 1 */ | |
/* | |
Advantages: | |
Won't have to muck with parent:: static junk | |
Disadvantage: | |
- Limits flexibility of the developer as they can't directly access the | |
property without going through the parent, which may be a net advantage from an OO perspective, dunno. | |
- Not what people would probably expect on how it works | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment