3

Sometimes I use __get or stdClass to convert arrays to object. But I can't decide I should stick to. I wonder which one is better and faster, any ideas?

class property 
{

    public function __get($name)
    {
        return (isset($this->$name)) ? $this->$name : null;
    }
}

$object = new property();
$object = new stdClass();

so if I use new property(), I will have a property object output,

property Object
(
....
)

while if I use new stdClass(), I will have a stdClass object output,

stdClass Object
(
....
)

so I can get the object data like this $item->title.

EDIT:

how I do the actual array to object conversion.

public function array_to_object($array = array(), $property_overloading = false)
    {

        # If $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) return $array;

        # Use property overloading to handle inaccessible properties, if overloading is set to be true.
        # Else use std object.
        if($property_overloading === true) $object = new property();
            else $object = new stdClass();

        foreach($array as $key => $value)
        {
            $key = (string) $key ;
            $object->$key = is_array($value) ? self::array_to_object($value, $property_overloading) : $value;
        }

        return $object;
    }
hakre
  • 193,403
  • 52
  • 435
  • 836
Run
  • 54,938
  • 169
  • 450
  • 748
  • I'm not familiar with this technique. You can use the property object you've created exactly as you would a stdClass object? – Eric G Nov 14 '12 at 01:31
  • yes you can. but I am not sure if it is better than stdClass object or not... – Run Nov 14 '12 at 01:33
  • You forgot to mention how you do the actual array to object conversion. – Ja͢ck Nov 14 '12 at 01:35
  • Do you have any benefits in using your own property class or just like I said as a data container? – David Müller Nov 14 '12 at 01:36
  • sorry David, I don't understand - what do you mean by a 'data container'? – Run Nov 14 '12 at 01:38
  • check this out - http://krisjordan.com/dynamic-properties-in-php-with-stdclass – Run Nov 14 '12 at 01:39
  • 1
    Take a look here, http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass Basically, all you do is store data in your class (which is what I mean by data container). There are no methods or whatsoever applied to your property class which would make it preferable to a stdClass. – David Müller Nov 14 '12 at 01:46

2 Answers2

3

First off, an (almost) empty class definition like you have is almost like an stdClass so there won't be any major issues by using either.

That said, one advantage your "named" class has over stdClass is that you can define what happens when a non-existent property is being accessed by taking advantage of the __get magic method.

class property
{
   public function __get($name)
   {
       return null;
   }
}

The above is a simpler rewrite of your original property class; when __get() is being called, you already know that $this->$name is not set. Although this won't cause a notice, it doesn't prevent a fatal error when you try to reference $obj->bla->bla where $obj->bla doesn't exist.

It might be more useful to throw an exception when a non-existent property is being accessed:

class property
{
   public function __get($name)
   {
       throw new Exception("Property $name is not defined");
   }
}

This allows your code to catch the exception before it turns into a fatal runtime error that stops your script altogether.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

If you just use your "property" class as a dumb data container, take stdClass or even an array.

David Müller
  • 5,291
  • 2
  • 29
  • 33