Subject: | This class is pretty useless. |
Summary: | Package rating comment |
Messages: | 6 |
Author: | iltar van der berg |
Date: | 2009-06-02 07:14:58 |
Update: | 2009-06-02 09:50:21 |
|
|
|
iltar van der berg rated this package as follows:
Utility: | Bad |
Consistency: | Good |
Examples: | Sufficient |
|
 iltar van der berg - 2009-06-02 07:14:58
This class is pretty useless. Why convert an array to an object? It has no extra use. If you want to, you can use ArrayObject in php 5, if you want to throw in an array, convert it recursively, build an extent to it (done it before to store things in a registry).
If you want to create getters and setters, you can do this:
<?php
class MyTest extends ArrayObject {
public function __call($function, $args) {
$function = strtolower($function);
$call = substr($function, 0, 3);
if($call == 'get') {
$var = str_replace('get', '', $function);
return $this->offsetGet($function);
// or return $this[$function];
} elseif ($call == 'set') {
$this->offsetSet($function, $args[0]);
// or $this[$function] = $args[0];
}
}
}
?>
Optional you can also replace someValue by some_value and things, but this is just an example... Like I said before, it currently does not have much to use...
 Ivan - 2009-06-02 08:31:41 - In reply to message 1 from iltar van der berg
This is the way I convert array to object, straightforward:
<?php
$argObj = (object) array('lebenslageKey' => $lebenslageKey,
'keInfo' => $keInfo,
'profilData' => $profilData,
'linkConfig' => $linkConfig);
?>
 iltar van der berg - 2009-06-02 08:48:33 - In reply to message 2 from Ivan
If I'm correct, it becomes a stdClass. Why don't just keep it an array?
 Sebastian Potasiak - 2009-06-02 09:41:09 - In reply to message 1 from iltar van der berg
I didn't know about ArrayObject class. My class maybe is pretty useless, but it can be used as ALTERNATIVE way to convert arrays to objects.
Like with frameworks - a lot of them have same functions but you're using this one, which is the easiest to use for you.
 Michael Burgess - 2009-06-02 09:41:23 - In reply to message 3 from iltar van der berg
There are a very limited number of reasons why you'd want an object, one of them is enabling fluid access after a call to a method...
class T {
public static function get() {
$array = array('a' => 'b');
return (object) $array;
}
}
T::get()->a; //not possible with an array returned
Or if you wrote a function which ordinarily took an object, and you wanted to reuse it with an array
That's all i can think of...
 iltar van der berg - 2009-06-02 09:50:21 - In reply to message 4 from Sebastian Potasiak
But that's still no reason to use objects as arrays. For one, objects are not iterateable by default, you need to implement Iterator for that.
If your code uses $a->b and you get $a['b'], you're doing 2 things wrong:
1. You should never have public properties, always make them protected or higher.
2. You should use $a['b'] instead of converting $a['b'] to $a->b
I have not seen any framework directly accessing public properties, they always use getters and setters. The way you do it, is just creating another workaround.
|