jeudi 18 octobre 2012

PHP get dynamic stdClass property

<?php
$book = new stdClass; 
$book->title = "Test title";
$book->author = "Laurent Guigo";
?>

Résultat :

object(stdClass)#147 (2) {
  ["title"]=>
  string(10) "Test title"
  ["author"]=>
  string(13) "Laurent Guigo"
}

<?php
$property = "title";
echo $book->{$property};die;
?>

Résultat :
Test title

Aucun commentaire:

Enregistrer un commentaire