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

时间:2013-10-17 21:15:39

标签: php function standards public strict

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

错误消息

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;
  }
}

?>

1 个答案:

答案 0 :(得分:2)

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

遵循以下部分通知:非静态方法KRecord :: new_record()告诉我们 在您的班级KRecord中,需要静态定义方法new_record,例如:

public function new_record($fields) {

只需要静态定义:

 public static function new_record($fields) {

您表示您已将其更改为静态,但问题中的代码并未表明此更改。