8
<?php
    class Stat
    {
        public $var1='H';
        public static $staticVar = 'Static var';


        static function check()
        {

            echo $this->var1;               
            echo "<br />".self::$staticVar ."<br />";
            self::$staticVar = 'Changed Static';
            echo self::$staticVar."<br />";
        }
        function check2()
        {
            Stat::check();
            echo $this->var1;           
            echo "b";
        }
    }

?>

Can i use it like this

$a = new Stat();
$a->check2();
Abin Manathoor Devasia
  • 1,945
  • 2
  • 21
  • 47
user1983198
  • 111
  • 1
  • 1
  • 2
  • 1
    How would a static method know what instance of an object it should be accessing to access non-static properties? The simple answer is "No". The more detailed answer is that it should issue a error "use of $this in a static context" – Mark Baker Jan 16 '13 at 10:12
  • If it _can_, then PHP is even more b0rked than I thought. Only an _instance method_ should be able to access _instance_ variables. – Alnitak Jan 16 '13 at 10:13
  • 1
    I believe this will throw an error. As you cannot access a non static property from a static method. In c# you could make the property static and access the static property from the static method, but as i recall i dont believe you can do that in php? but im happy to be corrected on that. – Nicholas King Jan 16 '13 at 10:15
  • @NicholasKing you can create static properties in PHP, it would basically transform the static class into a singleton, but, yeah, it's possible. Why does OP need a non-static property from within a static method is beyond me though, a static property would do just fine. – Mahn Jan 16 '13 at 10:23
  • Some of the people above need to get off there high horses. @Jan yes a user can try and see that $this isnt accessible inside a static method however maybe the user above thought that someone with more experience might know a way to do it. instead of giving the user grief maybe just explain that it isnt possible. – azzy81 May 16 '13 at 16:15

4 Answers4

10

No. A static method will not have access to $this (as there is no $this to talk about in a static context).

If you need a reference to the current object within the static method, it is not a static method. You can however access static properties and functions from a non-static method.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
5

As the other answers say, you can't use the instance methods in a static method. However, you can store a static property being an array of your instances. With some code like this:

private static $_instances = array();

public function __construct() {
    self::$_instances[] = $this;
}

This way, you can call a method on all the instances in a static method. With some code like this:

public static effItAll() {
    foreach (self::$_instances as $instance) {
        $instance->instanceMethod();
    }
}

Or you could also just store the last instance. Or some instance depending on the parameters. Whatever, you can just store any instance in a static property, thus being able to then call this instance in your static method.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
0

You just can't. The explanation is simple: there is no access to $this in a static method context. You have to find another design to achieve what you want :-)

astorije
  • 2,666
  • 2
  • 27
  • 39
  • 1
    You could do a $instance = new self(); so that you could call the non static variables, it goes against the static element purpose but you could do it haha – Carlos Arturo Alaniz Dec 07 '14 at 21:19
0

No. Static methods and instance objects can not work together in this way. However, you can pass a reference of your instance object to the static method:

static function check(&$classReference) //Adding class reference to function
{

    echo $classReference->var1;               
    echo "<br />".self::$staticVar ."<br />";
    self::$staticVar = 'Changed Static';
    echo self::$staticVar."<br />";
}
function check2()
{
    Stat::check($this); //Passing $this
    echo $this->var1;           
    echo "b";
}

However, just because it's possible doesn't mean it should be done. There's probably another way to achieve what you want in a much better OOP way.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102