简体   繁体   English

严格标准:公共静态功能:错误

[英]Strict Standards: Public Static Function :Errors

I'm having a bit of an issue. 我有点问题。 I just updated my dedicated server to PHP 5.4.17 and seem to be running into some strict standard issues. 我刚刚将我的专用服务器更新为PHP 5.4.17,似乎遇到了一些严格的标准问题。 I can't hide the errors due to the unique way that our server and scripts handle errors, and just changing "public function" to "public static function" doesn't seem to be doing anything at all. 由于我们的服务器和脚本处理错误的独特方式,我无法隐藏错误,只是将“公共函数”更改为“公共静态函数”似乎根本没有做任何事情。 Below is the snippet of code that is causing the error. 下面是导致错误的代码片段。 Any help is appreciated. 任何帮助表示赞赏。

ERROR MESSAGE 错误信息

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

Thanks, Thomas 谢谢,托马斯

actions_controller.php 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 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 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;
  }
}

?>

It's a notice , not really an error. 这是一个通知 ,而不是一个错误。 You've got an interesting server setup if you're not able to disable strict notices.... 如果你无法禁用严格的通知,你就有了一个有趣的服务器设置....

Following this part of the notice: Non-static method KRecord::new_record() tells us that in your class KRecord , the method new_record needs to be defined statically, such as: 以下部分通知: 非静态方法KRecord :: new_record()告诉我们在您的类KRecord ,需要静态定义new_record方法,例如:

public function new_record($fields) {

Simply need to be defined statically: 只需要静态定义:

 public static function new_record($fields) {

You indicate you've changed them to static, but your code in the question does not indicate that change. 您表明您已将其更改为静态,但问题中的代码并未表明该更改。

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

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