Access variable in another method from static method in same class

superphonic

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?

jeroen

$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;

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access protected variable of class inside static method of the same class?

From Dev

How do I access the variable of one method from another method within the same class C#?

From Dev

how to change the value of a static variable from a static method of another class

From Dev

Test static method that call another from same class

From Dev

accessing variable in a method to another method in same class

From Dev

Access parent class variable from method, used by another class

From Dev

PHP reference Class from variable with static method access

From Dev

What is the best way to refer to a static method from another static method of the same class in Rust?

From Dev

Access static method from static variable

From Javascript

Calling a method from another method in the same class

From Dev

How to pass a variable from one method to another in the same class?

From Dev

Accessing variable from another method within the same class

From Dev

Python: Access class method variable from another file

From Java

How do I access a non static method from main method in the same class?

From Dev

how to call a variable of another method in another method in same class in java

From Dev

How to reference static method from class variable

From Dev

PHP: is it possible to call a static class method from another static class?

From Dev

Passing variable from another file and then use variable within to another method within same class

From Dev

Accessing a variable of one method inside another method in the same class - Python

From Dev

using static member variable of a class inside a method of this same class

From Dev

Printing a value from another method in the same class

From Dev

Passing an ArrayList from a method to another in the same class?

From Java

Is it possible to access a variable from another method?

From Dev

VueJS access to a variable from another method

From Dev

Access location variable from another method

From Dev

Access a variable from one method in another

From Dev

Access an object from another class in private method

From Java

Access a child method from another child class

From Dev

Access a method from another class in Android

Related Related

  1. 1

    How to access protected variable of class inside static method of the same class?

  2. 2

    How do I access the variable of one method from another method within the same class C#?

  3. 3

    how to change the value of a static variable from a static method of another class

  4. 4

    Test static method that call another from same class

  5. 5

    accessing variable in a method to another method in same class

  6. 6

    Access parent class variable from method, used by another class

  7. 7

    PHP reference Class from variable with static method access

  8. 8

    What is the best way to refer to a static method from another static method of the same class in Rust?

  9. 9

    Access static method from static variable

  10. 10

    Calling a method from another method in the same class

  11. 11

    How to pass a variable from one method to another in the same class?

  12. 12

    Accessing variable from another method within the same class

  13. 13

    Python: Access class method variable from another file

  14. 14

    How do I access a non static method from main method in the same class?

  15. 15

    how to call a variable of another method in another method in same class in java

  16. 16

    How to reference static method from class variable

  17. 17

    PHP: is it possible to call a static class method from another static class?

  18. 18

    Passing variable from another file and then use variable within to another method within same class

  19. 19

    Accessing a variable of one method inside another method in the same class - Python

  20. 20

    using static member variable of a class inside a method of this same class

  21. 21

    Printing a value from another method in the same class

  22. 22

    Passing an ArrayList from a method to another in the same class?

  23. 23

    Is it possible to access a variable from another method?

  24. 24

    VueJS access to a variable from another method

  25. 25

    Access location variable from another method

  26. 26

    Access a variable from one method in another

  27. 27

    Access an object from another class in private method

  28. 28

    Access a child method from another child class

  29. 29

    Access a method from another class in Android

HotTag

Archive