【问题标题】:Strict Standards: Public Static Function :Errors严格标准:公共静态函数:错误
【发布时间】:2013-10-26 13:49:53
【问题描述】:

我有点问题。我刚刚将我的专用服务器更新到 PHP 5.4.17,似乎遇到了一些严格的标准问题。由于我们的服务器和脚本处理错误的独特方式,我无法隐藏错误,并且只是将“公共函数”更改为“公共静态函数”似乎根本没有做任何事情。下面是导致错误的代码的 sn-p。任何帮助表示赞赏。

错误信息

Strict Standards: Non-static method KRecord::new_record() should not be called statically, assuming $this from incompatible context in ../actions_controller.php on line 11
Strict Standards: Non-static method KRecord::sanitize_field_list() should not be called statically, assuming $this from incompatible context in ../krecord.php on line 70
Strict Standards: Non-static method KRecord::set_str() should not be called statically, assuming $this from incompatible context in ../krecord.php on line 72
Strict Standards: Non-static method KRecord::sanitize_field_list() should not be called statically in ../krecord.php on line 94
Strict Standards: Non-static method KRecord::set_str() should not be called statically in ../krecord.php on line 95

谢谢, 托马斯

actions_controller.php

require_once('../action.php');

class ActionsController {

public function ActionsController() {
  Action::init();
}
    public static function create($fields) {
  $action = Action::new_record($fields);
  return $action;
    }

    public function show($id) {
  $action = Action::find_by($id);
  return $action;
    }

    public function show_all($condition) {
   $action = Action::find_all_by($condition);
  return $action;
    }

    public function update($fields, $condition) {
  $action = Action::update($fields, $condition);
  return $action;
    }

    public function create_or_update($fields, $condition) {

  // $condition will be: WHERE `day_id` = 123 AND `type` = "lunch"

  $action = Action::find_by($condition);

  if ( $action ) {
     Action::update($fields, $condition);
  } else {
     // Remember to include all the necessary fields
     Action::new_record($fields);
  }

  return $action;
    }

    public function destroy($condition) {
  $action = Action::destroy($condition);
    }
}

actions.php

<?php
require_once('../krecord.php');

class Action extends KRecord {

public static function init() {
   KRecord::$tablename = 'mod_ets_actions';
   KRecord::$fieldlist = array('id', 'employee_id', 'action', 'parameters', 'action_time');
    }
}

krecord.php

    <?php

class KRecord {

public static $tablename;       // Name of the table
public static $fieldlist = array();     // The list of fields in the table (array)

public function KRecord() {
}

/* Cleans the fields passed into various functions.
  If the field is not in list given in the model, 
  this method strips it out, leaving only the correct fields.
*/
private function sanitize_field_list($fields) {
 $field_list = self::$fieldlist;
 //print_r($field_list);
    foreach ( $fields as $field => $field_value ) {
        if ( !in_array($field, $field_list) ) {
   //echo "<br/>$field is gone...<br/>";
            unset($fields[$field]);
        }
    }

//print_r($fields);

    return $fields;
}

// Setter for $tablename
public static function set_tablename($tname) {
    self::$tablename = $tname;
}

// Turns the key-value pairs into a comma-separated string to use in the queries
private function set_str($clean_fields) {
    $set_string = NULL;
    foreach ( $clean_fields as $field => $field_val ) {
        $set_string .= "`$field` = '$field_val', ";
    }

    $set_string = rtrim($set_string, ', ');
    return $set_string;
}

// Assembles the condition string
private function query_str($type, $clean_fields) {
    $fieldlist = self::$fieldlist;
    $update_str = NULL;
    $delim = NULL;

    foreach ($clean_fields as $field => $field_val) {
        if ( $type == 'where' ) {
            if ( isset($fieldlist[$field]['pkey']) ) {
                $delim = ' AND ';
                $update_str .= "$field = '$field_val'$delim ";
            }
        } elseif ( $type == 'update' ) {
            $delim = ', ';
            $update_str .= "$field = '$field_val'$delim ";
        }
    }

    $update_str = rtrim($update_str, $delim);
    return $update_str;
}

// Inserts new record into the database
public function new_record($fields) {
    $clean_fields = self::sanitize_field_list($fields); 
//echo "<br/>".self::set_str($clean_fields)."<br/>";
    $q = 'INSERT INTO ' . '`' . self::$tablename . '` SET ' . self::set_str($clean_fields) . ';';
  //echo "<br/>$q<br/>";
    $r = mysql_query($q) or die(mysql_error());
    return;
}

// An experimental method. Do not use without testing heavily
public static function insert_unless_exists($fields, $condition) {
    $clean_fields = self::sanitize_field_list($fields); 
$q = 'INSERT INTO ' . '`' . self::$tablename . '` SET ' . self::set_str($clean_fields);
$q .= ' ON DUPLICATE KEY UPDATE ' . self::set_str($clean_fields);
$q .= ' WHERE ' . $condition . ';';

/* Don't use replace.. it deletes rows. do some sort of insert/update deal. */
//$q = 'REPLACE INTO ' . '`' . self::$tablename . '` SET ' . self::set_str($clean_fields);
//echo "<br/>$q<br/>";
    $r = mysql_query($q) or die(mysql_error());
    return;
}

// Updates a record in the data table
public static function update($fields, $condition) {
 $clean_fields = self::sanitize_field_list($fields);
 $q = 'UPDATE `' . self::$tablename . '` SET ' . self::set_str($clean_fields) . ' WHERE ' . $condition .';';
 //echo "<br/>$q<br/>";
  $r = mysql_query($q) or die(mysql_error());
  return;
}

//Removes a record from the data table
public static function destroy($condition) {
    $q = 'DELETE FROM ' . self::$tablename . ' WHERE ' . $condition .';';
    $r = mysql_query($q);
    return;
}

// Finds one record using the given condition
public static function find_by($condition) {
    $q ='SELECT * FROM ' . '`' . self::$tablename . '`' . ' WHERE ' . $condition .';';
  //echo "<br/>$q<br/>";
  $r = mysql_query($q) or die(mysql_error());
    $obj = mysql_fetch_object($r);

    return $obj;
}

// Finds 1+ records in the table using the given conditions
public static function find_all_by($condition) {
    if ( empty($condition) ) {
        $where_str = null;
    } else {
        $where_str = ' WHERE ' . $condition;
    }

    $objs = array();
    $q ='SELECT * FROM ' . '`' .self::$tablename . '`' . $where_str . ';';
//echo "<br/>$q<br/>";
$r = mysql_query($q) or die(mysql_error());

    while ( $o = mysql_fetch_object($r) ) {
   if ( $o->id != '' ) 
      $objs[$o->id] = $o;
   else
     array_push($objs, $o);
    }

    return $objs;
  }
}

?>

【问题讨论】:

    标签: php function standards public strict


    【解决方案1】:

    这是一个通知,并不是真正的错误。如果您无法禁用严格通知,那么您的服务器设置很有趣......

    在这部分通知之后:非静态方法 KRecord::new_record() 告诉我们 在你的类KRecord中,需要静态定义方法new_record,如:

    public function new_record($fields) {
    

    只需要静态定义:

     public static function new_record($fields) {
    

    您表明您已将它们更改为静态,但您在问题中的代码并未表明该更改。

    【讨论】:

    • 我的错误。我复制了测试文件。我已经纠正了。它已经有: public static function create($fields) {
    • 对 - 但 create() 不是发出通知的函数,它是 new_record。
    • 所以我只是完全忽略了 new_record 的问题吗?我想我只是不明白两者之间的相关性。
    • 您能否快速尝试将静态声明添加到KRecord::new_Record() 方法中,看看是否无法清除通知?抱歉,我没有理解您的意思两者之间的相关性 - 两个是什么?
    • 我不明白KRecord::new_Record() 我假设你指的是Action::new_record($fields);
    猜你喜欢
    • 2023-03-31
    • 2013-03-31
    • 2011-10-07
    • 2015-05-08
    • 2013-09-15
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多