Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: Casting from parent class to child

Posted on 2010-10-06 16:02:09 by Teto

--001485f5e13a32d92c0491f339bd
Content-Type: text/plain; charset=UTF-8

Hi,

I'm trying to extends the base class DateTime.
parent::createFromFormat("H.i d.m.Y", $string); returns a DateTime and I
want to convert it into a DateTimePlus (my own extended class). What's the
best way to do this plz ?

Some code:
class DateTimePlus extends DateTime

{
static function setFromGUIDateAndTime($date,$time ){

$string = $time.' '.$date;
//echo 'str:'.$string;
$res = parent::createFromFormat("H.i d.m.Y", $string);
return (DateTimePlus)parent::createFromFormat("H.i d.m.Y", $string);

}


Thk you

matt

--001485f5e13a32d92c0491f339bd--

Report this message

#2: Re: Casting from parent class to child

Posted on 2010-10-06 18:30:14 by David Harkness

--001485f6c760c43bda0491f54ae6
Content-Type: text/plain; charset=ISO-8859-1

Casting does not change an object. You must copy the relevant value(s) from
the object returned into a new DateTimePlus. Since DateTime's constructor
takes only a string, and I assume it won't accept your format directly,
you're better off converting the string into a Unix timestamp and creating a
new object from that. However, I leave that optimization to you. The
following code is sufficient:

$plus = new DateTimePlus();
$plus.setTimestamp(parent::createFromFormat("H.i d.m.Y",
$string).getTimestamp());
return $plus;

David

--001485f6c760c43bda0491f54ae6--

Report this message

#3: Re: Casting from parent class to child

Posted on 2010-10-08 18:50:52 by Nathan Rixham

David Harkness wrote:
> Casting does not change an object. You must copy the relevant value(s) from
> the object returned into a new DateTimePlus. Since DateTime's constructor
> takes only a string, and I assume it won't accept your format directly,

unless you implement __toString I believe (not tested)

> you're better off converting the string into a Unix timestamp and creating a
> new object from that. However, I leave that optimization to you. The
> following code is sufficient:
>
> $plus = new DateTimePlus();
> $plus.setTimestamp(parent::createFromFormat("H.i d.m.Y",
> $string).getTimestamp());
> return $plus;
>
> David
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message

#4: Re: Casting from parent class to child

Posted on 2010-10-08 19:42:08 by Andrew Ballard

On Fri, Oct 8, 2010 at 12:50 PM, Nathan Rixham <nrixham@gmail.com> wrote:
> David Harkness wrote:
>>
>> Casting does not change an object. You must copy the relevant value(s)
>> from
>> the object returned into a new DateTimePlus. Since DateTime's constructor
>> takes only a string, and I assume it won't accept your format directly,
>
> unless you implement __toString I believe (not tested)
>

IMO, that would be a truly useful feature to add if you were extending
DateTime anyway.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message