I Upgrade php7.4 to php8.2
but i get these error in my SSP Class
Fatal error: Uncaught Error: Non-static method SSP::get_field() cannot be called statically in C:\xampp\htdocs\classes\ssp.inc.php:394
My SSP Class
<?php
class SSP {
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
$column_name = self::get_column_name($column);
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[$column['dt']] = $column['formatter']($data[$i][$column_name], $data[$i] );
}
else {
$row[$column['dt']] = $data[$i][$column_name];
}
}
$out[] = $row;
}
return $out;
}
static function db ( $conn )
{
if ( is_array( $conn ) ) {
return self::sql_connect( $conn );
}
return $conn;
}
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = self::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.self::get_column_name($column).'` '.$dir;
}
}
if ( count( $orderBy ) ) {
$order = 'ORDER BY '.implode(', ', $orderBy);
}
}
return $order;
}
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "CONVERT(".$column['db']." USING utf8) LIKE ".$binding;
}
}
}
// Individual column filtering
if ( isset( $request['columns'] ) ) {
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "CONVERT(".$column['db']." USING utf8) LIKE ".$binding;
}
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
static function table($in_table)
{
if (is_string($in_table)){
return "`$in_table`";
}
$dtables = array();
foreach ($in_table as $t_info){
if (!isset($t_info['table'])){
continue;
}
$from_table="";
if ($t_info['join_type']){
$from_table.=$t_info['join_type'] . " JOIN ";
}
$from_table.= "`".$t_info['table']."`".(($t_info['as']) ? " AS `".$t_info['as']."`" : "");
if ($t_info['join_on']){
$from_table.=" ON ".$t_info['join_on'];
}
$dtables[] = $from_table." ";
}
if ( count( $dtables ) ) {
return implode($dtables);
}
return "";
}
function get_field($field, $field_params=null){
$parts = explode("." , $field);
for ( $i=0, $len=count($parts) ; $i<$len ; $i++ ) {
$parts[$i] = "`$parts[$i]`";
}
$fval = implode(".", $parts);
if ($field_params && $field_params['group_func']){
$fval = $field_params['group_func']."(".$fval.")";
}
return $fval;
}
function get_column_name($column){
if ($column['as']){
return $column['as'];
}
$parts = explode("." , $column['db']);
if (count($parts) > 1){
return $parts[1];
}
return $parts[0];
}
static function simple ( $request, $conn, $p_table, $primaryKey, $columns)
{
$bindings = array();
$db = self::db( $conn );
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
//emayskiy: p_table may be array
$table = self::table($p_table);
$primaryKey = self::get_field($primaryKey);
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT ".implode(", ", self::pluck($columns, 'db'))."`
FROM $table
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db, $bindings,
"SELECT COUNT($primaryKey
FROM $table
$where"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db,
"SELECT COUNT($primaryKey)
FROM $table"
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => isset ( $request['draw'] ) ?
intval( $request['draw'] ) :
0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
static function complex ( $request, $conn, $p_table, $primaryKey, $columns, $whereResult=null, $whereAll=null, $groupBy=null, $having=null)
{
$bindings = array();
$db = self::db( $conn );
$whereAllSql = '';
//emayskiy: Get field for query
$primaryKey = self::get_field($primaryKey);
//emayskiy: p_table may be array
$table = self::table($p_table);
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
$whereResult = self::_flatten( $whereResult );
$whereAll = self::_flatten( $whereAll );
if ( $whereResult ) {
$where = $where ?
$where .' AND '.$whereResult :
'WHERE '.$whereResult;
}
if ( $whereAll ) {
$where = $where ?
$where .' AND '.$whereAll :
'WHERE '.$whereAll;
$whereAllSql = 'WHERE '.$whereAll;
}
//emayskiy: Add group and having
$groupBy = ($groupBy) ? ' GROUP BY '.$groupBy .' ' : '';
$having = ($having) ? ' HAVING '.$having .' ' : '';
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT ".implode(", ", self::pluck($columns, 'db'))."
FROM $table
$where
$groupBy
$having
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db, $bindings,
"SELECT COUNT($primaryKey)
FROM $table
$where"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db, $bindings,
"SELECT COUNT($primaryKey)
FROM $table ".
$whereAllSql
);
$recordsTotal = $resTotalLength[0][0];
return array(
"draw" => isset ( $request['draw'] ) ?
intval( $request['draw'] ) :
0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
static function sql_connect ( $sql_details )
{
try {
$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']};port={$sql_details['port']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
$db->exec("set names utf8");
}
catch (PDOException $e) {
self::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
self::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll( PDO::FETCH_BOTH );
}
static function fatal ( $msg )
{
echo json_encode( array(
"error" => $msg
) );
exit(0);
}
static function bind ( &$a, $val, $type )
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = (($prop=='db')? self::get_field($a[$i][$prop], $a[$i]).(($a[$i]['as']) ? " AS `".$a[$i]['as']."`" : ""):$a[$i][$prop]);
}
return $out;
}
static function _flatten ( $a, $join = ' AND ' )
{
if ( ! $a ) {
return '';
}
else if ( $a && is_array($a) ) {
return implode( $join, $a );
}
return $a;
}
}
What's wrong ? any why when update it's stopped Please Very Important to fixing it
Upgrade from php7.4 to php8.2
You can safely change
function get_field
tostatic function get_field
to fix the problem. Almost every function isSSP
is already static andget_field
doesn't use$this
or reference any class variables, so it can be made static like everything else in the class. Same with theget_column_name
function.Newer PHP versions started enforcing more strict rules with regards to calling a non-static method (e.g.
function foo
) statically (e.g.$object::foo()
). Before, this would be allowed even though it was being called in a different way than it was defined.For more information, see https://www.php.net/manual/en/language.oop5.static.php.