7

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?

1
  • 1
    make the $noms array static also Commented Aug 10, 2012 at 11:59

3 Answers 3

27

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) {
Sign up to request clarification or add additional context in comments.

4 Comments

already tried and when I call it by self::noms it says 'nom' is undefined
Nice shot ;) I hopped there was a solution to keep it not static (it is cleaner) but that's enough :) thanks
I think, I your case it's the cleanest way you can reach :)
Solved my problem! :)
3

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'];

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.