简体   繁体   English

PHP从静态方法调用非静态方法

[英]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 我已经在Java中阅读了一些有关此的问题,但是我找不到在PHP中寻找的东西-尽管这些非常接近: php静态方法问题
Call method non-static with static in PHP 在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. 我有一个如下的会话类(由session::init()调用),但是我无法解决如何避免收到错误消息的问题。

<?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. 我只希望__is_session_started函数由init()函数调用-因此它不必是静态的或公共的-但我不确定如何引用它。

If I replace QUERY HERE with 'self::' the page loads, but I get this error: 如果我将“ QUERY HERE ”替换为“ self ::”,则页面会加载,但出现此错误:

 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. 但是,如果我尝试使用$ this-> _ is_session_started()来非静态地引用该方法,则会收到致命错误,因为静态init()函数尚未实例化会话类。

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. 我可以使_is_session_started方法成为静态方法,或者只是使我的init函数更长,但是我很想知道为什么会这样。

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. $this关键字是这里的特殊调味料。 Since you do not have an object instance in a static method, ie no $this , you cannot call a non-static method: 由于您在静态方法中没有对象实例,即没有$this ,因此无法调用非静态方法:

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

If your function uses $this and requires an object instance, you cannot call it statically. 如果您的函数使用$this并且需要一个对象实例,则不能静态调用它。 If your function does not use $this and does not require an object instance, make it static and call it statically. 如果您的函数不使用$this并且不需要对象实例,请使其成为staticstatic调用。

You have to declare the method _is_session_started as static. 您必须将方法_is_session_started声明为静态。

private static function _is_session_started() {

Then call it with 然后用

self::_is_session_started();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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