TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   array to object (http://www.talkphp.com/advanced-php-programming/2515-array-object.html)

Gibou 03-24-2008 01:31 AM

array to object
 
Hi !

I would like to quickly transform an array into an object and whatever the keys presents into the array. I want it dynamic and not static, key by key.
I've thought to this method:

PHP Code:

<?php
foreach($params as $key => $val)
{
    if(isset(
$this->$key))
        
$this->$key $val;
}
?>

but it doesn't work. It cause no error but the $this->$key never exists.
My array is like:
id => 1
title => "test Site"
url => "http://www.monsite.com"
story => "blablabla"

and my object attributes are:

PHP Code:

<?php
private $id;
private 
$title;
private 
$url;
private 
$story;
?>

Do you have any idea ?

Thank you !

TlcAndres 03-24-2008 01:55 AM

Private attributes can't be accessed outside of the object.

Gibou 03-24-2008 03:42 AM

ah yes I know, I've forgotten to say that the foreach loop is in the constructor of the class containing the attributes.

sjaq 03-24-2008 08:27 AM

you should look at the __get and __set magic methods ;)

Gibou 03-24-2008 10:17 AM

__get and __set make easy access to private members whithout having to declare as many getters and setters as attributes. My problem is not an access one because this foreach loop will be used ONLY in the constructor of the class, never elsewhere.

I give the code with more precisions this time:

PHP Code:

<?php
public function __construct($params)
{
    if(!empty(
$params) && is_array($params))
    {
        foreach(
$params as $key => $val)
        {
            if(isset(
$this->$key))
                
$this->$key $val;
        }
    }
}
    
private 
$id;
private 
$title;
private 
$url;
private 
$story;
?>


Alan @ CIT 03-24-2008 10:54 AM

Hi Gibou,

The problem with your code is:

PHP Code:

if(isset($this->$key)) 

This will return false for each of your variables as they are set to NULL. You can overcome this problem by giving them a default value - ie, something like the following:

PHP Code:

<?php
class Test
{

    private 
$id 0;
    private 
$title '';
    private 
$url '';
    private 
$story '';
    
    public function 
__construct($params)
    {

        if(!empty(
$params) && is_array($params))
        {
            foreach(
$params as $key => $val)
            {
                if (isset(
$this->$key))
                {
                    
$this->$key $val;
                }
            }
        }
    }

}

$args = array(
    
'id' => 1,
    
'title' => 'My Article',
    
'url' => 'http://www.talkphp.com',
    
'story' => 'something here...'
);

$test = new Test($args);

var_dump($test);

Will result in:

Code:

object(Test)#1 (4) {
  ["id:private"]=>
  int(1)
  ["title:private"]=>
  string(10) "My Article"
  ["url:private"]=>
  string(22) "http://www.talkphp.com"
  ["story:private"]=>
  string(17) "something here..."
}

Alan

Gibou 03-24-2008 11:10 AM

Oh yes ! It works !
Strange... When you test isset($_GET["toto"]) with page.php?toto, it works so I was sure it was the same in this case.

Thank you Alan !

Alan @ CIT 03-24-2008 11:16 AM

The quirks of PHP :-) In your test above, PHP gives $_GET['toto'] a default string value of "". If a $_GET variable wasn't passed in the URL, it would have the value of NULL in your script.

Alan

Salathe 03-24-2008 12:04 PM

You can check if a property exists with the appropriately named property_exists (PHP Manual Page). Note: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

As a side note, your original post didn't make it clear but it might be useful, have you tried something like: $object = (object) $arrray; since you did originally ask how "to quickly transform an array into an object".

Alan @ CIT 03-24-2008 12:10 PM

Thanks Salathe, never knew about property_exists(). Handy little function.

Alan

Gibou 03-24-2008 01:25 PM

How, yes, thank you Salathe, it's exactly what I was looking for :)

PS: I can't do that because my foreach loop is in the constructor.
If I write $this = (object)$params, php write this:

Fatal error: Cannot re-assign $this in blabla\news.php on line 17

Salathe 03-24-2008 06:07 PM

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';
    }




All times are GMT. The time now is 09:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0