Access private variable from static function in php

I have 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 usual, I cannot get it with $ this, because there is no instance of a static method.

What is the correct syntax to get $ noms inside my static function?

+5
source share
3 answers

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) {
+13
source

To access the $ noms array, make it static, you will do this:

private static $noms = array();

Then you access it like this:

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

0
source

You need to make noms static and access it through self :: $ noms.

0
source