0

Why can't I get access to my "incSessionCount" function inside my "newSession" function?

class Session {
    private $_num_session = 0;

    private function incSessionCount() {
        $this->_num_session++;
    }

    public static function newSession($key, $value) {
        if( !isset( $_SESSION[$key] ) ) {
            $_SESSION[$key] = $value;
            $this->incSessionCount();
            return true;
        } else {
            return false;
        }
    }
}

I just played around, like making incSessionCount() public and so on... And then I thought, that it must be even accessible, when it's set to private ...

It's possible, that I missed a useful article, which should have helped me, but finally I ended up asking.

So why doesn't this work?

Jon Lamer
  • 408
  • 6
  • 12

5 Answers5

6

The problem is that your newSession is static, thus you are not supposed to call instance methods from it.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
2

I suppose you're trying to do:

Session::newSession($key, $value);

instead of

$session = new Session();
$session->newSession($key, $value);

The error is not because you're calling a private method from within a public one, but because you're using $this instead of self.

$this special variable represents the current instance object while self represents the class itself.

Trent
  • 5,785
  • 6
  • 32
  • 43
  • Note: most of the time [you want to use `static` over of `self`](http://stackoverflow.com/questions/5197300/new-self-vs-new-static) ^^. – moonwave99 Nov 13 '13 at 16:55
  • @moonwave99, I don't agree, most of the time you actually want to use `self` instead of `static` which is designed to solve the late static binding "problems" which itself have specific use cases. – Trent Nov 13 '13 at 16:57
1

If you enable error display and set error reporting level to E_ALL, you will see the problem is about using $this in a wrong context.

See below theses little modifications to do what you want, and check theses pages about

class Session {
    private $_num_session = 0;
    private static $inst = null;

    public static function instance(){
      if (!static::$inst)
        static::$inst = new Session();
      return static::$inst;
    }


    private function incSessionCount() {
        $this->_num_session++;
    }

    public static function newSession($key, $value) {
        if( !isset( $_SESSION[$key] ) ) {
            $_SESSION[$key] = $value;
            Session::getInstance()->incSessionCount();
            return true;
        } else {
            return false;
        }
    }
}

You can look for design pattern and singleton on internet, and use magic __clone() to forbid more than one instance

I only found the german version of the documentation, I don't know why : http://de.php.net/manual/de/language.oop5.patterns.php

EDIT: Check this link about design patterns : http://www.phptherightway.com/pages/Design-Patterns.html

Asenar
  • 6,732
  • 3
  • 36
  • 49
0

Remember, that static methods are binded with the class. Non-static methods are binded with the instance (when you do something like $instance = new MyClass();). But when you call something on the static context, you don't have to have any instance.

It's the same, when you want to call something on instance($this), because in static context doesn't exist any instance.

-1

The problem is the public method is static and you are trying to use a method for an instantiated object. In a static method the $this variable refers to other static methods and properties only.

Itzalive
  • 370
  • 4
  • 14
  • 1
    In `static` methods called statically `$this` doesn't exist at all. – deceze Nov 13 '13 at 16:52
  • Calling `$this` from a static context "may" work in old version of PHP or where the error level is low (ie not using strict) but it is fundamentally wrong. – Trent Nov 13 '13 at 16:55