Skip to content

Instantly share code, notes, and snippets.

@cpriest
Last active December 10, 2015 10:28
Show Gist options
  • Save cpriest/4420904 to your computer and use it in GitHub Desktop.
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.
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