Logo SNS - Web et Informatique

SNS - Web et Informatique

Samedi 24 Juillet 2021
06 25 23 34 56
12150, Sévérac-le-Château
 08h00/23h00 - 7j/7
Être rappelé

  Création de site web professionnel : Offre exclusive à 480 €    Consultez notre démonstration  

Utiliser une méthode comme fonction de comparaison dans une classe - Use a method as a comparison function in a class

Utiliser une méthode comme fonction de comparaison dans une classe - Use a method as a comparison function in a class

Publié le .

Il peut être utile d'utiliser une fonction de comparaison dans une classe en PHP. Mais si on suit le style procédural, on va tomber sur une erreur dy type " cannot redeclare ... " Et c'est logique. Voici comment utiliser une méthode de classe pour effectuer une fonction de tri, dans des tableaux imbriqués, par exemple.

It can be useful to use a compare function in a class in PHP. But if we follow the procedural style, we will come across a type error "cannot redeclare ..." And it makes sense. Here's how to use a class method to perform a sort function, in nested arrays, for example.


<?php
/**
* @version 0.1.0
* @license GNU GPL
* @link www.sns.pm
* @author Raphaël Castello - SNS Web & Informatique
* // need to sort an array()
* Function My_Class::get_players( $ARRAY_USERS );
* Function My_Class::sort_array( $a , $b );
*/

class My_Class {

const ARRAY_USERS = array(
array( 'name' => 'Jamie', 'score' => 35, 'time_played' => 3600 ),
array( 'name' => 'Abdul', 'score' => 48, 'time_played' => 10800 ),
array( 'name' => 'Evan', 'score' => 76, 'time_played' => 14400 ),
array( 'name' => 'Carla', 'score' => 68, 'time_played' => 7200 ),
array( 'name' => 'Debora', 'score' => 54, 'time_played' => 1800 )
);

/**
* Function My_Class::get_players( $ARRAY_USERS );
* @ access public
* @ static
* // must be an array returned by a data base
* @ Param const ARRAY_USERS
* return all users classed
*/
public static function get_players( $ARRAY_USERS ){

// Use usort() with a public class
usort($ARRAY_USERS, "My_Class::sort_array" );

// See output in bottom
echo json_encode( $ARRAY_USERS ,
JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT );
}
/**
* END Function lists::get_players( $ARRAY_USERS );
* return all users classed
*/

/**
* Function My_Class::sort_array( $a , $b );
* @ access public
* @ static
* @ Param. $data_type -> 'alpha' (alphabetical) / 'num' (number)
* @ Param. $order -> 'ASC' / 'DESC' - inverse order
* @ Param. $key -> 'name' , 'score, etc ..
* Sort imbricateds arrays
*/
public static function sort_array( $a , $b ){

$data_type = 'num' ; // 'alpha'
$order = 'DESC' ; // 'ASC'
$key = 'score'; // 'name'

// SORT NUMERIC VALUES
if( $data_type == 'num' && $order == 'DESC' ){
return $b[$key] - $a[$key];
}
if( $data_type == 'num' && $order == 'ASC' ){
return $a[$key] - $b[$key];
}

// SORT ALPHABETICAL VALUES
if( $data_type == 'alpha' && $order == 'ASC' ){
return $a[$key] > $b[$key];
}
if( $data_type == 'alpha' && $order == 'DESC' ) {
return $b[$key] > $a[$key];
}

}
/**
* END Function My_Class::sort_array( $a , $b );
* Sort imbricateds arrays
*/

}
// END class My_Class

// Launch Method
My_Class::get_players( My_Class::ARRAY_USERS );

?>

// Output :
[
{
"name": "Evan",
"score": 76,
"time_played": 14400
},
{
"name": "Carla",
"score": 68,
"time_played": 7200
},
{
"name": "Debora",
"score": 54,
"time_played": 1800
},
{
"name": "Abdul",
"score": 48,
"time_played": 10800
},
{
"name": "Jamie",
"score": 35,
"time_played": 3600
}
]