错误信息 严格标准:非静态方法不应在 php 中静态调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4684454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:58:19  来源:igfitidea点击:

Error message Strict standards: Non-static method should not be called statically in php

phpstatic-members

提问by shin

I have the following php. However when I see the index.php I get the following error message.

我有以下 php.ini 文件。但是,当我看到 index.php 时,我收到以下错误消息。

Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12

严格标准:非静态方法 Page::getInstanceByName() 不应在第 12 行的 /var/www/webworks/index.php 中静态调用

I am hoping someone can tell me how to fix the problem.

我希望有人能告诉我如何解决这个问题。

index.php

索引.php

// { common variables and functions
include_once('ww.incs/common.php');
$page=isset($_REQUEST['page'])?$_REQUEST['page']:'';
$id=isset($_REQUEST['id'])?(int)$_REQUEST['id']:0;
...

// { get current page id
if(!$id){
    if($page){ // load by name
        $r=Page::getInstanceByName($page);
        if($r && isset($r->id))$id=$r->id;
    }
    if(!$id){ // else load by special
        $special=1;
        if(!$page){
            $r=Page::getInstanceBySpecial($special);
            if($r && isset($r->id))$id=$r->id;
        }
    }
}

// { load page data
if($id){
    $PAGEDATA=(isset($r) && $r)?$r : Page::getInstance($id);
}
else{
    echo '404 thing goes here';
    exit;
}
...
...

ww.incs/common.php

ww.incs/common.php

<?php
require dirname(__FILE__).'/basics.php';
...
...

ww.incs/basics.php

ww.incs/basics.php

session_start();
if(!function_exists('__autoload')){
    function __autoload($name) {
        require $name . '.php';
    }
}
...
...

Page.php

页面.php

class Page{
    static $instances             = array();
    static $instancesByName     = array();
    static $instancesBySpecial   = array();
    function __construct($v,$byField=0,$fromRow=0,$pvq=0){
        # byField: 0=ID; 1=Name; 3=special
        if (!$byField && is_numeric($v)){ // by ID
            $r=$fromRow?$fromRow:($v?dbRow("select * from pages where id=$v limit 1"):array());
        }
        else if ($byField == 1){ // by name
            $name=strtolower(str_replace('-','_',$v));
            $fname='page_by_name_'.md5($name);
            $r=dbRow("select * from pages where name like '".addslashes($name)."' limit 1");
        }
        else if ($byField == 3 && is_numeric($v)){ // by special
            $fname='page_by_special_'.$v;
            $r=dbRow("select * from pages where special&$v limit 1");
        }
        else return false;
        if(!count($r || !is_array($r)))return false;
        if(!isset($r['id']))$r['id']=0;
        if(!isset($r['type']))$r['type']=0;
        if(!isset($r['special']))$r['special']=0;
        if(!isset($r['name']))$r['name']='NO NAME SUPPLIED';
        foreach ($r as $k=>$v) $this->{$k}=$v;
        $this->urlname=$r['name'];
        $this->dbVals=$r;
        self::$instances[$this->id] =& $this;
        self::$instancesByName[preg_replace('/[^a-z0-9]/','-',strtolower($this->urlname))] =& $this;
        self::$instancesBySpecial[$this->special] =& $this;
        if(!$this->vars)$this->vars='{}';
        $this->vars=json_decode($this->vars);
    }
    function getInstance($id=0,$fromRow=false,$pvq=false){
        if (!is_numeric($id)) return false;
        if (!@array_key_exists($id,self::$instances)) self::$instances[$id]=new Page($id,0,$fromRow,$pvq);
        return self::$instances[$id];
    }
    function getInstanceByName($name=''){
        $name=strtolower($name);
        $nameIndex=preg_replace('#[^a-z0-9/]#','-',$name);
        if(@array_key_exists($nameIndex,self::$instancesByName))return self::$instancesByName[$nameIndex];
        self::$instancesByName[$nameIndex]=new Page($name,1);
        return self::$instancesByName[$nameIndex];
    }
    function getInstanceBySpecial($sp=0){
        if (!is_numeric($sp)) return false;
        if (!@array_key_exists($sp,$instancesBySpecial)) $instancesBySpecial[$sp]=new Page($sp,3);
        return $instancesBySpecial[$sp];
    }

回答by Gordon

Your methods are missing the statickeyword. Change

您的方法缺少static关键字。改变

function getInstanceByName($name=''){

to

public static function getInstanceByName($name=''){

if you want to call them statically.

如果你想静态调用它们。

Note that static methods (and Singletons) are death to testability.

请注意,静态方法(和单例)是可测试性的死亡

Also note that you are doing way too much work in the constructor, especially all that querying shouldn't be in there. All your constructor is supposed to do is set the object into a valid state. If you have to have data from outside the class to do that consider injecting it instead of pulling it. Also note that constructors cannot return anything. They will always return void so all these return falsestatements do nothing but end the construction.

另请注意,您在构造函数中做了太多工作,尤其是所有查询不应该在那里。您的构造函数应该做的就是将对象设置为有效状态。如果您必须从班级外部获取数据才能做到这一点,请考虑注入它而不是拉取它。还要注意构造函数不能返回任何东西。它们将始终返回 void,因此所有这些return false语句只会结束构造。

回答by Pea

I think this may answer your question.

我想这可以回答你的问题。

Non-static method ..... should not be called statically

非静态方法 ..... 不应静态调用

If the method is not static you need to initialize it like so:

如果该方法不是静态的,则需要像这样初始化它:

$var = new ClassName();
$var->method();

Or, in PHP 5.4+, you can use this syntax:

或者,在 PHP 5.4+ 中,您可以使用以下语法:

(new ClassName)->method();

回答by Andrés Frías

Try this:

尝试这个:

$r = Page()->getInstanceByName($page);

It worked for me in a similar case.

它在类似的情况下对我有用。

回答by ulas korpe

use className->function(); instead className::function() ;

使用 className->function(); 而是 className::function() ;

回答by Ravi Krishnan

If scope resolution :: had to be used outside the class then the respective function or variable should be declared as static

如果范围解析 :: 必须在类之外使用,则相应的函数或变量应声明为静态

class Foo { 
        //Static variable 
        public static $static_var = 'static variable'; 
        //Static function 
        static function staticValue() { return 'static function'; } 

        //function 
        function Value() { return 'Object'; } 
} 



 echo Foo::$static_var . "<br/>"; echo Foo::staticValue(). "<br/>"; $foo = new Foo(); echo $foo->Value();

回答by Tomas

return falseis usually meant to terminate the object creation with a failure. It is as simple as that.

return false通常意味着以失败终止对象创建。它是如此简单。

回答by Lorand

Instead of using the instance with the scope resolution operator :: because it wasn't defined like static function.

而不是将实例与范围解析运算符 :: 一起使用,因为它不像静态函数那样定义。

$r=Page::getInstanceByName($page);

change it to :

将其更改为:

$r=Page->getInstanceByName($page);

And it will work like a charm.

它会像魅力一样发挥作用。