PSR-16 question

75 views
Skip to first unread message

Alice Wonder

unread,
Feb 24, 2018, 2:56:13 PM2/24/18
to PHP Framework Interoperability Group
I have a personal cache class that would be cake to port to use PSR-16, but PSR-16 also defines two exception interfaces.

Is it really necessary I extend those two interfaces rather than extend a different exception interface?

zetacomponents base package has an abstract exceptions class I sometimes extend.

If I have to implement the interface in PSR-16 to be compliant than to use that abstract class I would have to change that abstract class to implement the PSR-16 exception interfaces instead of just \Exception and it strikes me that that is just, well, absurd since the PSR-16 interfaces don't do anything or even extend \Exception.

Am I not understanding the purpose of those interfaces?

Chuck Burgess

unread,
Feb 24, 2018, 3:02:18 PM2/24/18
to php...@googlegroups.com
Purpose of the interfaces is to make those exact exception types *catchable*... that's all.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/85594732-4a0a-4058-a263-a48db4738e70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Dragoonis

unread,
Feb 26, 2018, 6:35:15 AM2/26/18
to php...@googlegroups.com
Hi Alice,

It's recommended not to throw \Exception but throw your own cache exception class, which implement's PSR-16's CacheException.

"""
use Psr\SimpleCache\CacheException;
class MyProjectsCacheException extends Exception implements CacheException
"""

This way you'll be able to continue to catch(Exception $e) in general, but also be specific and typehint on CacheException if you need to.

I hope that answers your question.

Many thanks,
Paul


Alice Wonder

unread,
Feb 26, 2018, 12:09:07 PM2/26/18
to PHP Framework Interoperability Group
Thank you,

I got it figured out. I have not personally worked with implementing interfaces before, had seen classes that implemented them but wasn't aware they could both extend another class *and* implement an interface. So it was ignorance confusing me.
I have it working now extending \InvalidArgumentException which from the name of the interface is what I'm guessing was intended, which lets me catch them with \InvalidArgumentException which I'm guessing is also what was intended.


On Monday, February 26, 2018 at 3:35:15 AM UTC-8, Paul Dragoonis wrote:
Hi Alice,

It's recommended not to throw \Exception but throw your own cache exception class, which implement's PSR-16's CacheException.

"""
use Psr\SimpleCache\CacheException;
class MyProjectsCacheException extends Exception implements CacheException
"""

This way you'll be able to continue to catch(Exception $e) in general, but also be specific and typehint on CacheException if you need to.

I hope that answers your question.

Many thanks,
Paul

On Sat, Feb 24, 2018 at 8:02 PM, Chuck Burgess <demon...@gmail.com> wrote:
Purpose of the interfaces is to make those exact exception types *catchable*... that's all.
On Feb 24, 2018 13:56, "Alice Wonder" <alicedo...@gmail.com> wrote:
I have a personal cache class that would be cake to port to use PSR-16, but PSR-16 also defines two exception interfaces.

Is it really necessary I extend those two interfaces rather than extend a different exception interface?

zetacomponents base package has an abstract exceptions class I sometimes extend.

If I have to implement the interface in PSR-16 to be compliant than to use that abstract class I would have to change that abstract class to implement the PSR-16 exception interfaces instead of just \Exception and it strikes me that that is just, well, absurd since the PSR-16 interfaces don't do anything or even extend \Exception.

Am I not understanding the purpose of those interfaces?

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/85594732-4a0a-4058-a263-a48db4738e70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages