O
O
Optimus2016-12-10 15:08:18
PHP
Optimus, 2016-12-10 15:08:18

How to pass variables to static method?

I need to pass one variable to the method, he has the rest necessary for work in the class, but he does not see them and says Undefined variable about b and c
How best to do in this case? Put them inside the method directly? What if they are needed in another class method?

class MyClass
{
    private $b = 1;
    private $c = 2;
    
    public static function data($a, $b, $c){
        $res = $a + $b + $c;
        return $res;
    }
}

$res = MyClass::data(25);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eudj1n, 2016-12-10
Pyan @marrk2

You can declare variables as static:

class MyClass
{
    private static $b = 1;
    private static $c = 2;

    public static function data($a){
        $res = $a + static::$b + static::$c;
        return $res;
    }
}

$res = MyClass::data(25);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question