0

I'm trying to use myVar inside my of a method's function. I have already tried adding global but still nothing. I know this is probably basic but I can't seem to find it.

class myClass{
  
  public $myVar;

  public function myFunction() {
  
    function myInnerFunction() {
      //how do I use this variable here
      echo $this->myVar; 
    }
  }
}

Whenever I try using $this I get this error: 'Using $this when not in object context in...'

miken32
  • 42,008
  • 16
  • 111
  • 154
alexdmejias
  • 1,399
  • 4
  • 19
  • 34
  • use private or protected methods. You just defined a global function. You can even try using `create_function` (http://php.net/manual/en/function.create-function.php) – Populus Jul 10 '13 at 17:13
  • `function myInnerFunction` makes that function *global* (but only after `myFunction` is called). Is `myInnerFunction` being called from outside of `myFunction`? Where does `myInnerFunction` get called from? – gen_Eric Jul 10 '13 at 17:31

5 Answers5

3

You should use $this->myVar

See the PHP Documentation - The Basics

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}
?>

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs

Update:

In your new code sample, myInnerFunction is a nested function and is not accessible until the myFunction method is called. Once the myFunction method is called, the myInnerFunction becomes part of the global scope.

Maybe this is what you are looking for:

 class myClass{

  public $myVar;

  public function myFunction() {

  }

  function myInnerFunction() {
    //how do I use this variable here
    echo $this->myVar; 
  }

}
Community
  • 1
  • 1
  • Sorry. I have already tried that but forgot to put it in the snippet above. I just updated the code to reflect that – alexdmejias Jul 10 '13 at 16:51
1

Inner functions like myInnerFunction are always global in scope, even if they are defined inside of a member function in a class. See this question for another similar example

So, to PHP, the following are (almost) equivalent:

class myClass{

  public $myVar;

  public function myFunction() {

    function myInnerFunction() {
      //how do I use this variable here
      echo $this->myVar; 
    }
  }
}

And

class myClass{

  public $myVar;

  public function myFunction() {

  }
}

function myInnerFunction() {
   //how do I use this variable here
   echo $this->myVar; 
}

Hopefully the second example illustrates why $this is not even in scope for myInnerFunction. The solution is simply to pass the variable as a parameter to the function.

Community
  • 1
  • 1
Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

Pass it as an argument to the inner function.

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
0

You can use ReflectionProperty:

$prop = new ReflectionProperty("SimpleClass", 'var');

Full example:

class myClass{

  public $myVar;

  public function myFunction() {

    function myInnerFunction() {
      //how do I use this variable here
      $prop = new ReflectionProperty("SimpleClass", 'myVar');
    }
  }
}
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
0

The solution above is good when you need each instance to have an own value. If you need all instances to have a same you can use static:

class myClass
{
  public static $myVar = "this is my var's value";

  public function myClass() {
      echo self::$myVar;
  }
}

new myClass();

see here

Cody Tookode
  • 862
  • 12
  • 22