Вопрос

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

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

?>
Это было полезно?

Решение

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:

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 с атрибуция
Не связан с StackOverflow
scroll top