Re: Extending SplDoublyLinkedList with specific child types and avoiding strict notices
| From: | AshleySheridan | Date: | Mon, 20 Mar 2017 19:33:45 +0000 |
| Subject: | Re: Extending SplDoublyLinkedList with specific child types and avoiding strict notices | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326026@lists.php.net to get a copy of this message | ||
On Mon, 2017-03-20 at 13:21 -0600, Ryan Pallas wrote:
> I don't believe this is possible due to not being able to narrow type
> hints
> for arguments in extended classes. However, you could just remove the
> typehint, and check yourself, something like:
>
> public class push($item)
> {
> if (!($item instanceof
> SpecificChildTypeClass::class))
> throw new TypeError(sprintf(
> 'Argument 1 passed to %s::push() must be
> of
> the
> type %s, %s given',
> static::class,
> SpecificChildTypeClass::class,
> gettype($item) != 'object' ?
> gettype($item) :
> get_class($item)
> ));
> parent::push($item);
> }
> >
That's what I was afraid of. It's annoying, but understandable given
how it's implemented. I might look then at implementing a custom
iterator, as that might better suit my needs here.
Thanks,
Ash
http://www.ashleysheridan.co.uk