Accessing private variable from static function in

2019-06-21 22:18发布

I've got a private variable in my class

private $noms = array(
        "HANNY",
        "SYS",
        "NALINE"
);

I want to access it from a static method:

public static function howManyNom($searchValue){

        $ar = $this->noms;

        foreach($ar as $key => $value) {

...

But as normal I cant retrieve it with $this because there's no instance on a static method.

What's the right syntax to get $noms inside my static function?

标签: php static
3条回答
放荡不羁爱自由
2楼-- · 2019-06-21 22:57

You have to make the noms static, too and access it via self::$noms.

查看更多
Fickle 薄情
3楼-- · 2019-06-21 22:59

Make this attribute static too!

private static $noms = array(
    "HANNY",
    "SYS",
    "NALINE"
);


public static function howManyNom($searchValue){

    $ar = self::$noms;

    foreach($ar as $key => $value) {
查看更多
放荡不羁爱自由
4楼-- · 2019-06-21 23:19

To access the $noms array make it static, you do that like so:

private static $noms = array();

You then access that like so:

self::$noms['some key'];

查看更多
登录 后发表回答