Welcome! Log In Create A New Profile

Advanced

[PHP] Extending SplDoublyLinkedList with specific child types and avoiding strict notices

Posted by AshleySheridan 
Hopefully the title says it all. Basically I'm extending the
SplDoublyLinkedList as it's better on memory than arrays, is object
orientated, and it can be extended with additional functionality more
easily. It's that last part that is causing some issues. I'm extending
to create a list class that only takes specific children. This example
will throw PHP strict notices when run, which I ideally want to avoid:

<?php
class SomeSpecificListClass extends \SplDoublyLinkedList
{
public class push(SpecificChildTypeClass $item)
{
parent::push($item);
}
}

Has anyone else tried something like this before and found a better
way?


Thanks,
Ash

http://www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
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);
}

On Mon, Mar 20, 2017 at 12:57 PM, AshleySheridan <[email protected]>
wrote:

> Hopefully the title says it all. Basically I'm extending the
> SplDoublyLinkedList as it's better on memory than arrays, is object
> orientated, and it can be extended with additional functionality more
> easily. It's that last part that is causing some issues. I'm extending
> to create a list class that only takes specific children. This example
> will throw PHP strict notices when run, which I ideally want to avoid:
>
> <?php
> class SomeSpecificListClass extends \SplDoublyLinkedList
> {
> public class push(SpecificChildTypeClass $item)
> {
> parent::push($item);
> }
> }
>
> Has anyone else tried something like this before and found a better
> way?
>
>
> Thanks,
> Ash
>
> http://www.ashleysheridan.co.uk
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login