gpt4 book ai didi

php - 错误消息 严格标准 : Non-static method should not be called statically in php

转载 作者:IT老高 更新时间:2023-10-28 11:49:22 29 4
gpt4 key购买 nike

我有以下 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

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

index.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

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

ww.incs/basics.php

session_start();
if(!function_exists('__autoload')){
function __autoload($name) {
require $name . '.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];
}

最佳答案

您的方法缺少 static keyword .改变

function getInstanceByName($name=''){

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

如果你想静态调用它们。

请注意,静态方法(和 Singletons)是 death to testability .

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

关于php - 错误消息 严格标准 : Non-static method should not be called statically in php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4684454/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com