php - Most efficient way of accessing a private variable of another class? -
there many methods around allow access private variables of class, efficient way?
for example:
i have class details in:
class something{ private $details = ['password' => 'stackoverflow',]; }
in class need access them, example (although wouldn't work since variable isn't in scope class):
class accesssomething{ public function getsomething(){ print($something->details['password']); } }
would function enough within class "something" used access class?:
public function getsomething($x, $y){ print $this->$x['$y']; }
you should using proper getters/setters
in classes allow access otherwise restricted data.
for example
a class
class aclass { private $member_1; private $member_2; /** * @return mixed */ public function getmember1() { return $this->member_1; } /** * @param mixed $member_1 */ public function setmember1( $member_1 ) { $this->member_1 = $member_1; } /** * @return mixed */ public function getmember2() { return $this->member_2; } /** * @param mixed $member_2 */ public function setmember2( $member_2 ) { $this->member_2 = $member_2; } }
which called follows:
$newclass = new aclass(); echo $newclass->getmember1(); echo $newclass->getmember2();
Comments
Post a Comment