superphonic
superphonic

Reputation: 8074

Access variable in another method from static method in same class

I am looking for the best/correct way to do the following:

myClass::getSomething('stuff');

class myClass
{

    public static function getSomething($var) {

        $obj = new static();
        $obj->var = $var;

        $obj->somethingElse();

    }

    public function somethingElse() {

        // I need to access $obj->var in here

    }

}

Do I pass $obj to somethingElse(), is that the right way?

Upvotes: 3

Views: 188

Answers (1)

jeroen
jeroen

Reputation: 91734

$obj is an instance of myClass: It has - among others - a method somethingElse() and you just added a property $var.

So in your method you can access the property directly:

public function somethingElse() {

    $the_contents_of_var = $this->var;

}

Upvotes: 2

Related Questions