PHP:访问 Class 中的私有变量
PHP: Get Acces To Private Variable In Class
过去几个小时我一直在 PHP 做一个项目,我遇到了一个问题。
问题是我不知道如何访问 class 中的私有变量,而且我在网上找不到它。
示例:
<?php
class Example{
private $age;
public function __construct() {
$age = 14;
$this->checkAge();
}
private function checkAge() {
if($this->$age > 12)
echo "welcome!";
}
}
$boy = new Example();
?>
据我所知,我应该可以使用 $this->$age 访问变量,但它不起作用。
谢谢。
编辑:在很棒的 stackoverflooooooooow 社区的帮助下让它工作,这就是工作的样子。
<?php
class Example{
private $age;
public function __construct() {
$this->age = 14;
$this->checkAge();
}
private function checkAge() {
if($this->age > 12)
echo "welcome!";
}
}
$boy = new Example();
?>
看看这个方法。
首先: 创建在私有 $attributes 数组中存储和检索数据的实体,并使用魔法 __set(), __get() 您也可以这样做: $object->变量 = 123
第二个: 用人类 class 扩展实体并添加一些特定于 child class 的函数(例如 hasValidAge()):
<?php
class Entity {
private $attributes;
public function __construct($attributes = []) {
$this->setAttributes($attributes);
}
public function setAttribute($key, $value) {
$this->attributes[$key] = $value;
return $this;
}
public function setAttributes($attributes = []) {
foreach($attributes AS $key => $value) {
$this->setAttribute($key, $value);
}
}
public function getAttribute($key, $fallback = null) {
return (isset($this->attributes[$key]))?
$this->attributes[$key] : $fallback;
}
public function __get($key) {
return $this->getAttribute($key);
}
public function __set($key, $value) {
$this->setAttribute($key, $value);
}
}
class Human extends Entity {
public function __construct($attributes = []) {
$this->setAttributes($attributes);
$this->checkAge();
}
public function hasValidAge() {
return ($this->getAttribute('age') > 12)? true : false;
}
}
$boy = new Human(['name' => 'Mark', 'age' => 14]);
if($boy->hasValidAge()) {
echo "Welcome ".$boy->name."!";
}
?>
p.s。我从构造函数中删除了 echo "Welcome!" 部分,因为从模型 object 中执行 echo 并不酷,在我们的示例中,Human 是 Entity 的模型。
过去几个小时我一直在 PHP 做一个项目,我遇到了一个问题。
问题是我不知道如何访问 class 中的私有变量,而且我在网上找不到它。
示例:
<?php
class Example{
private $age;
public function __construct() {
$age = 14;
$this->checkAge();
}
private function checkAge() {
if($this->$age > 12)
echo "welcome!";
}
}
$boy = new Example();
?>
据我所知,我应该可以使用 $this->$age 访问变量,但它不起作用。
谢谢。
编辑:在很棒的 stackoverflooooooooow 社区的帮助下让它工作,这就是工作的样子。
<?php
class Example{
private $age;
public function __construct() {
$this->age = 14;
$this->checkAge();
}
private function checkAge() {
if($this->age > 12)
echo "welcome!";
}
}
$boy = new Example();
?>
看看这个方法。
首先: 创建在私有 $attributes 数组中存储和检索数据的实体,并使用魔法 __set(), __get() 您也可以这样做: $object->变量 = 123
第二个: 用人类 class 扩展实体并添加一些特定于 child class 的函数(例如 hasValidAge()):
<?php
class Entity {
private $attributes;
public function __construct($attributes = []) {
$this->setAttributes($attributes);
}
public function setAttribute($key, $value) {
$this->attributes[$key] = $value;
return $this;
}
public function setAttributes($attributes = []) {
foreach($attributes AS $key => $value) {
$this->setAttribute($key, $value);
}
}
public function getAttribute($key, $fallback = null) {
return (isset($this->attributes[$key]))?
$this->attributes[$key] : $fallback;
}
public function __get($key) {
return $this->getAttribute($key);
}
public function __set($key, $value) {
$this->setAttribute($key, $value);
}
}
class Human extends Entity {
public function __construct($attributes = []) {
$this->setAttributes($attributes);
$this->checkAge();
}
public function hasValidAge() {
return ($this->getAttribute('age') > 12)? true : false;
}
}
$boy = new Human(['name' => 'Mark', 'age' => 14]);
if($boy->hasValidAge()) {
echo "Welcome ".$boy->name."!";
}
?>
p.s。我从构造函数中删除了 echo "Welcome!" 部分,因为从模型 object 中执行 echo 并不酷,在我们的示例中,Human 是 Entity 的模型。