php - Call external function from within class that accesses private properties -



php - Call external function from within class that accesses private properties -

i'm trying create code little drier doing follow in php:

function accessor($obj, $property) { homecoming $obj->$property; } class someclass { private $variable; function variable() { homecoming accessor($this, 'variable'); } } $some = new someclass; echo $some->variable();

the above code throws error because external function can't access private variable. code simplified, farther coding create more useful.

i'm not sure if possible sure nice!

what want traits (php 5.4+)- practically pasted parent classes, can access private state:

trait variablethingy { function accessor($property) { homecoming $this->$property; } } class test { utilize variablethingy; private $variable = 15; function variable() { homecoming $this->accessor("variable"); } }

but no, it's not nice. it's atrocious. , whole code rather pointless. if want dry, go public variable. dry , encapsulated mutually exclusive.

php class

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -