par
xTG » 10 déc. 2011, 14:50
Je suis tombé sur une interrogation devant un code auquel j'avais mal pensé...
J'utilise sort() sur un array d'objet, cela a l'air de fonctionner mais je ne sais pas pourquoi...
Si quelqu'un a des lumières à apporter là dessus. Car j'ai eu beau retourner la documentation je n'ai pas trouvé d'informations relatives à mon interrogation.
Voici l'objet en question qui est ensuite inséré dans un array sur lequel je fais le tri.
Est-ce grâce à la fonction compare() que j'ai implémenté ? Ou bien il récupère tout simplement $value qui est public ?
Ou bien est-il plus bourrin que cela et il serialize l'objet pour le comparer ?
class Node{
/*
* @type (int)
*/
public $value = null;
/*
* Constructor
*/
function __construct( $value ){
if( !isSet($value) )
throw new InvalidArgumentException("Node : no value.");
if( is_object($value) )
throw new InvalidArgumentException("Incorrect use of Node class, extends it to use an object as a node.");
if( !is_int($value) )
throw new InvalidArgumentException("Node : only integer value.");
$this->value = $value;
}
/*
* Return the value
* @param (int)
*/
public function value(){
return $this->value;
}
/*
* Compare two nodes
* @param (Node)
* @return (int) gap
*/
public function compare( $node ){
if( !($node instanceof Node) )
throw new InvalidArgumentException("Argument is not a Node object.");
return $this->compareValue($node->value());
}
/*
* Compare a value with the Node value
* @param (int)
* @return (int) gap
*/
public function compareValue( $value ){
if( gettype($this->value) !== gettype($value) )
return false;
return ($value - $this->value);
}
}
Je suis tombé sur une interrogation devant un code auquel j'avais mal pensé...
J'utilise sort() sur un array d'objet, cela a l'air de fonctionner mais je ne sais pas pourquoi...
Si quelqu'un a des lumières à apporter là dessus. Car j'ai eu beau retourner la documentation je n'ai pas trouvé d'informations relatives à mon interrogation.
Voici l'objet en question qui est ensuite inséré dans un array sur lequel je fais le tri.
Est-ce grâce à la fonction compare() que j'ai implémenté ? Ou bien il récupère tout simplement $value qui est public ?
Ou bien est-il plus bourrin que cela et il serialize l'objet pour le comparer ? :mrgreen:
[php]class Node{
/*
* @type (int)
*/
public $value = null;
/*
* Constructor
*/
function __construct( $value ){
if( !isSet($value) )
throw new InvalidArgumentException("Node : no value.");
if( is_object($value) )
throw new InvalidArgumentException("Incorrect use of Node class, extends it to use an object as a node.");
if( !is_int($value) )
throw new InvalidArgumentException("Node : only integer value.");
$this->value = $value;
}
/*
* Return the value
* @param (int)
*/
public function value(){
return $this->value;
}
/*
* Compare two nodes
* @param (Node)
* @return (int) gap
*/
public function compare( $node ){
if( !($node instanceof Node) )
throw new InvalidArgumentException("Argument is not a Node object.");
return $this->compareValue($node->value());
}
/*
* Compare a value with the Node value
* @param (int)
* @return (int) gap
*/
public function compareValue( $value ){
if( gettype($this->value) !== gettype($value) )
return false;
return ($value - $this->value);
}
}[/php]