On Mon, Sep 5, 2016 at 5:49 AM, Davey Shafik <m...@daveyshafik.com> wrote:

>
> > On Sep 5, 2016, at 02:38, Nicolas Grekas <nicolas.gre...@gmail.com>
> wrote:
> >
> > Hello,
> >
> > It looks like we miss a way to check by reflection if a function/method
> has
> > strict types enabled or not.
> >
> > We'd need to do this in Symfony to generate a file with concatenated
> > classes, but split "declare_strict=1" classes out so that they don't
> break
> > (because there is no way to create a single file that contains both
> strict
> > and non-strict classes unfortunately also).
> >
> > Would it be possible to expose some flavor of ZEND_ACC_STRICT_TYPES to
> > userland?
>
> Strict types are only enforced from the CALL site. A library author cannot
> dictate that their code be called using strict types or not, only the file
> actually calling the library methods can. Though, regardless, the type
> hints do ensure that the value received is of the type specified, you just
> won't know if it was coerced or not.
>
> Just like when you import a namespaced class and alias it, the alias only
> works in the file the "use " statement is in.
>
> Therefore, you cannot actually author strict or non-strict classes period,
> unless they are only called in the file in which they are defined.
>


Actually you can, in PHP 5 even.  This sort of thing is what the assert()
statement is for.


public function stringsOnly( $var ) {
  assert(is_str($var), 'Strings Only!');
}

This does have the weakness of not working when assertions are turned off.
If $var is a form of user input then an exception test is called for, and
likely should be done before stringsOnly in this example gets worked with.

Reply via email to