On Wed, 23 Jan 2019 at 15:43, Claude Pache <claude.pa...@gmail.com> wrote:

> So, since you didn’t see, here are some practical usages of settype():
>
> <?php
> function foo($bar) {
>     // $bar is supposed to be either a string, or a list of strings
>     settype($bar, 'array');
>     // ...
> }
>
> function qux($id) {
>      if (!(is_int($id) || is_string($id) && ctype_digit($id)))
>         throw new \TypeError;
>     settype($id, 'int');
>     // ...
> }
>
> class Foo implements SeekableIterator {
>     function seek(/* int */ $position): void {
>         // NOTE: we cannot use int typehint here, because PHP7.1 requires
> mixed
>         settype($position, 'int');
>         // ...
>     }
> }
> ?>
>

All these examples could very well use a typecast and or one of the *val
functions


> Here are a usage of settype() with a non-constant second parameter: Define
> a utility function that cast a value to a given type when it is not null:
>
> <?php
> function cast_opt($val, $type) {
>     if ($val !== null)
>         settype($val, $type);
>     return $val;
> }
>
> $foo = cast_opt($bar, 'int') // equivalent to: $foo = $bar === null ? null
> : (int) $bar;
> ?>
>
> Sure, one can avoid settype() and do it the complicated way, but why? What
> is the issue with settype(), so that you want to deprecate it?
>

You didn't necessarely need to specify an example with a non-constant
second parameter
because you already gave valid arguments as why not to deprecate it, just
that I didn't see
them. I do appreciate the example however.

Why I want*ed* to deprecate is because I personally didn't see any usage
before,
you provided a valid usage and I don't see the point of deprecating it
anymore.
Maybe that wasn't clear from my previous reply. Because indeed removing
settype
brings back the same issue that I raised with gettype.
As it currently stands the only way I can see how settype can possibly be
deprecated
is ih PHP introduces a ReflectionVar classe which, like said in my initial
email, seems
like total overkill when a simple function exists.


Hope this clears everything up

Best regards

George P. Banyard

Reply via email to