Thread: array to object
View Single Post
Old 03-24-2008, 06:07 PM   #12 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

My second point was more along the lines of:

PHP Code:
$array = array('id'    => 1
               
'title' => 'My Title'
               
'url'   => 'http://mysite.com/myurl'
               
'story' => 'This is my story');
$object = (object) $array;

var_dump($object);

/*

object(stdClass)#1 (4) {
  ["id"]=>
  int(1)
  ["title"]=>
  string(8) "My Title"
  ["url"]=>
  string(23) "http://mysite.com/myurl"
  ["story"]=>
  string(16) "This is my story"
}

*/ 
That resulting $object object is therefore the equivalent of an instance of:
PHP Code:
class myClass {

    public 
$id;
    public 
$title;
    public 
$url;
    public 
$story;

    public function 
__construct()
    {
        
$this->id    1;
        
$this->title 'My Title';
        
$this->url   'http://mysite.com/myurl';
        
$this->story 'This is my story';
    }

Salathe is offline  
Reply With Quote