简体   繁体   中英

php calling a non static method from a static method

I've read a few questions on this in Java but I couldn't find what I was looking for in PHP - though these were quite close: php static methods question
Call method non-static with static in PHP

Is it possible to call a non static method from within a static?

I've a session class as below (called by session::init() ) but I can't work out how to avoid getting error messages.

<?php 
class session {

    public static function init(){
    if (**QUERY HERE**_is_session_started() === FALSE ) {
//      if (session_status() == PHP_SESSION_NONE) {
            @session_start();   
        }   else {
            echo "Session already started<br/>";
        }
    }   


private function _is_session_started() {
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

}

I only ever want the _is_session_started function to be called by the init() function - so it doesn't need to be static or public - but I'm not sure how to reference it.

If I replace QUERY HERE with 'self::' the page loads, but I get this error:

 PHP Strict Standards:  Non-static method session::_is_session_started() should not be called statically in /Applications/MAMP/htdocs/mvc/libs/session.php on line 5

But if I try to reference the method non-statically by using $this->_is_session_started() then I get a fatal error because the static init() function hasn't instantiated the session class.

So I'm thinking there must be another way to do this. I could make the _is_session_started method static or just make my init function longer, but I'm keen to understand why this is.

Thanks

It doesn't make any sense to want to call a non-static method from a static one. To call a non-static method, you need an object instance . When you call a method statically, you have no object instance. I hope the discrepancy is obvious.

The point of non-static methods is that they have access to instance-specific data:

class Foo {

    public $bar;

    public function baz() {
        echo $this->bar;
    }

}

$a = new Foo;
$b = new Foo;

$a->bar = 'Hello';
$b->bar = 'World';

$a->baz();
$b->baz();

The $this keyword is the special sauce here. Since you do not have an object instance in a static method, ie no $this , you cannot call a non-static method:

Foo::baz();  // now what?

If your function uses $this and requires an object instance, you cannot call it statically. If your function does not use $this and does not require an object instance, make it static and call it statically.

You have to declare the method _is_session_started as static.

private static function _is_session_started() {

Then call it with

self::_is_session_started();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM