如何使用magic方法__set查看给定的数组NAME

问题描述 投票:0回答:1

如果我的实例中存在意外的数组定义,如何使用魔术方法__set访问给定的数组NAME?

例如,我想回显出已提交的数组名称:

class App {

__set(){

// echo array name

}

$app = new App();
$app->arrayName->['key'=>'val'];

如何回显arrayName或为提交的数组设置新名称?

php class oop methods set
1个回答
0
投票

__set()有两个参数,名称和值,请参阅PHP : Property Overloading

public function __set($name, $value){
    if(is_array($value)) { echo "$name is an array"; }
}

然后设置数组:

$app = new App();
$app->arrayName = ['key'=>'val'];

然后__set()将显示arrayName is an array

© www.soinside.com 2019 - 2024. All rights reserved.