Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Conversation

@parnas
Copy link

@parnas parnas commented Jul 28, 2014

No description provided.

@adamaveray
Copy link

FYI this method isn't actually "incorrectly" non-static – you can call it either way. Both of these are valid:

$seconds = $interval->toSeconds();
$seconds = DateInterval::toSeconds($interval);

It might seem strange, but it lets you call ->toSeconds() directly on a Herrara DateInterval instance, and also call ::toSeconds($interval) if you have a regular PHP DateInterval instance. In PHP you can call any instance method statically, and as long as that method doesn't try and call anything on $this it'll work. It feels a bit hacky and most IDEs will complain, but it's technically ok!

@martijndwars
Copy link

@adamaveray my IDE complains, but also when running doctrine commands from the CLI I get:

  [ErrorException]                                                                                              
  Non-static method Herrera\DateInterval\DateInterval::toSeconds() should not be called statically, assuming $this from incompatible context                                                                                

so PHP is actually complaining (PHP 5.5.9-1ubuntu4.9)

@adamaveray
Copy link

@martijndwars The environment you're running in is promoting warnings to exceptions, so that's why it's failing. The code would run in a standard environment...producing warnings, but still running. It's definitely not ideal though. Honestly I think your best bet is to just copy the whole DateInterval class, make your own version, and change it to be more like this:

namespace Some\Namespace;

class DateInterval extends DateInterval {
    // ...
    public function toSeconds(){
        return static::intervalToSeconds($this);
    }
    public static function intervalToSeconds(\DateInterval $interval){
        // All the code from the current `::toSeconds()`, without the first if statement/block
    }
}

(And while you're at it you might as well do the same for the ::toSpec() method)

If you do end up doing this let me know, I'd switch to that version!

@martijndwars
Copy link

Thanks for the quick reply and explanation @adamaveray! I ended up copying this PR, because it seems to be more maintained than this repo (and it has more chance of being merged into Doctrine DBAL)

That PR uses a different approach of storing DateInterval's (as spec strings instead of seconds), but my only goal was to map DateIntervals in Doctrine and I don't care about how they are persisted.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants