Casting objects.
am 18.01.2010 16:04:53 von Richard Quadling
Hello.
I've got an object via odbc_fetch_object (which is of stdClass).
I have an class structuresRemovalReason.
I want to cast the response of odbc_fetch_object to structuresRemovalReason.
The only way I can see of doing this is to not use odbc_fetch_object,
but odbc_fetch_assoc and then iterate the array and populate a new
structuresRemovalReason.
Or an I missing a trick. I'm pretty sure I am.
Regards,
Richard Quadling.
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Casting objects.
am 18.01.2010 16:49:06 von Shawn McKenzie
Richard Quadling wrote:
> Hello.
>
> I've got an object via odbc_fetch_object (which is of stdClass).
>
> I have an class structuresRemovalReason.
>
> I want to cast the response of odbc_fetch_object to structuresRemovalReason.
>
> The only way I can see of doing this is to not use odbc_fetch_object,
> but odbc_fetch_assoc and then iterate the array and populate a new
> structuresRemovalReason.
>
> Or an I missing a trick. I'm pretty sure I am.
>
> Regards,
>
> Richard Quadling.
>
Something like this in your class or another class may work (not
tested). You might even fetch an array and send it to this method. I
think either should work:
class structuresRemovalReason {
static public function cast(structuresRemovalReason $object) {
return $object;
}
}
$row = structuresRemovalReason::cast(odbc_fetch_object($result));
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Casting objects.
am 18.01.2010 17:03:33 von Shawn McKenzie
Shawn McKenzie wrote:
> Richard Quadling wrote:
>> Hello.
>>
>> I've got an object via odbc_fetch_object (which is of stdClass).
>>
>> I have an class structuresRemovalReason.
>>
>> I want to cast the response of odbc_fetch_object to structuresRemovalReason.
>>
>> The only way I can see of doing this is to not use odbc_fetch_object,
>> but odbc_fetch_assoc and then iterate the array and populate a new
>> structuresRemovalReason.
>>
>> Or an I missing a trick. I'm pretty sure I am.
>>
>> Regards,
>>
>> Richard Quadling.
>>
>
> Something like this in your class or another class may work (not
> tested). You might even fetch an array and send it to this method. I
> think either should work:
>
> class structuresRemovalReason {
>
> static public function cast(structuresRemovalReason $object) {
> return $object;
> }
> }
>
> $row = structuresRemovalReason::cast(odbc_fetch_object($result));
>
Never mind, that was stupid. I saw that somewhere before, but obviously
it doesn't work.
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Casting objects.
am 18.01.2010 17:51:53 von Shawn McKenzie
Shawn McKenzie wrote:
>>
> Never mind, that was stupid. I saw that somewhere before, but obviously
> it doesn't work.
>
I found some code, maybe I redeem myself?
static public function cast(&$object, $class=__CLASS__){
if(class_exists($class)) {
$object = unserialize(
preg_replace('/^O:[0-9]+:"[^"]+":/i',
'O:'.strlen($class).':"'.$class.'":',
serialize($object)));
}
}
$row = odbc_fetch_object($result);
structuresRemovalReason::cast($row);
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Casting objects.
am 19.01.2010 10:27:34 von Richard Quadling
2010/1/18 Shawn McKenzie :
> Shawn McKenzie wrote:
>>>
>> Never mind, that was stupid. I saw that somewhere before, but obviously
>> it doesn't work.
>>
>
> I found some code, maybe I redeem myself?
>
> static public function cast(&$object, $class=3D__CLASS__){
>
> Â Â Â Â if(class_exists($class)) {
> Â Â Â Â Â Â Â Â $object =3D unseri=
alize(
> Â Â Â Â Â Â Â Â Â Â =C2=
=A0 Â preg_replace('/^O:[0-9]+:"[^"]+":/i',
> Â Â Â Â Â Â Â Â Â Â =C2=
=A0 Â 'O:'.strlen($class).':"'.$class.'":',
> Â Â Â Â Â Â Â Â Â Â =C2=
=A0 Â serialize($object)));
> Â Â Â Â }
> }
>
> $row =3D odbc_fetch_object($result);
> structuresRemovalReason::cast($row);
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
That's an interesting approach.
Though I'm going to use reflection. I realise I need to hold some of
the data in strings where the data is not a string (datetimes for
example).
So, using a docblock with a customtag and this seems to be working just fin=
e.
Extended ReflectionClass and ReflectionProperty.
Thanks,
Richard.
--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Casting objects.
am 19.01.2010 22:45:11 von Carlos Medina
Richard Quadling schrieb:
> 2010/1/18 Shawn McKenzie :
>> Shawn McKenzie wrote:
>>> Never mind, that was stupid. I saw that somewhere before, but obviously
>>> it doesn't work.
>>>
>> I found some code, maybe I redeem myself?
>>
>> static public function cast(&$object, $class=__CLASS__){
>>
>> if(class_exists($class)) {
>> $object = unserialize(
>> preg_replace('/^O:[0-9]+:"[^"]+":/i',
>> 'O:'.strlen($class).':"'.$class.'":',
>> serialize($object)));
>> }
>> }
>>
>> $row = odbc_fetch_object($result);
>> structuresRemovalReason::cast($row);
>>
>> --
>> Thanks!
>> -Shawn
>> http://www.spidean.com
>>
> That's an interesting approach.
>
> Though I'm going to use reflection. I realise I need to hold some of
> the data in strings where the data is not a string (datetimes for
> example).
>
> So, using a docblock with a customtag and this seems to be working just fine.
>
> Extended ReflectionClass and ReflectionProperty.
>
> Thanks,
>
> Richard.
>
>
Hi,
i think it will be important since more PHP Developer are using Design
Patterns. I think the cast functionality should be implemented once...
Regards
Carlos
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php