Jon Lamer
Jon Lamer

Reputation: 428

PHP - Why can't I use private method inside public methods?

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?

Upvotes: 0

Views: 1811

Answers (5)

user2988785
user2988785

Reputation: 1

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.

Upvotes: 0

Asenar
Asenar

Reputation: 7010

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

Upvotes: 1

Trent
Trent

Reputation: 5877

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.

Upvotes: 2

moonwave99
moonwave99

Reputation: 22817

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

Upvotes: 6

Itzalive
Itzalive

Reputation: 390

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.

Upvotes: -1

Related Questions