Re: [PHP-DEV] [Discussion] Variable Type Declaration Before Usage

2023-11-07 Thread Thomas Gutbier

Am 05.11.2023 um 14:37 schrieb Oladoyinbo Vincent:

Hello Internals,

Local based variable declaration before usage is available in most
languages like javascript, python, and many more. php has this, but it's
only available while working/dealing with oop related.


Somewhat ironically, one could say that typed scalar variables are 
already supported,

albeit with a somewhat cumbersome syntax.

Instead of

 ```php

int $intVar;

```
you write

```php
class TypedInt {
    public static int $intVar = 0;
}
$intVar = ::$intVar;

echo $intVar; // 0

$intVar = 3;
echo $intVar; // 3

$intVar = 'x'; // Fatal error: Uncaught TypeError
```

https://3v4l.org/GFNgp#v8.2.11

Greetings
Thomas Gutbier

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php



Re: [PHP-DEV] [Discussion] Variable Type Declaration Before Usage

2023-11-06 Thread Marcos Marcolin

Local based variable declaration before usage is available in most
languages like javascript, python, and many more. php has this, but it's
only available while working/dealing with oop related.


Hello Oladoyinbo,

I'll contribute an observation, correct me if I'm wrong.

In Python or Javascript it is not possible to declare types, as they are 
dynamic languages, similar to PHP.

What you can do in Python is write type annotations, but it doesn't change its 
type at run time.

```python
myVar: int = 5;
print(myVar); # The output is 5
myVar = 'Marcos';
print(myVar); # The way out is Marcos
myVar = True;
print(myVar); # The output is True
```

As for Javascript, it doesn't even have type annotations, you need to use 
`JSDoc` to document, or use `Typescript` to use types.

Hug.

---
Marcos Marcolin
Software Engineer | PHP
www.marcosmarcolin.com.br


Re: [PHP-DEV] [Discussion] Variable Type Declaration Before Usage

2023-11-06 Thread Robert Landers
On Mon, Nov 6, 2023 at 3:08 AM David Gebler  wrote:
>
> On Sun, Nov 5, 2023 at 1:37 PM Oladoyinbo Vincent 
> wrote:
>
> > Hello Internals,
> >
> > I was wondering if php could introduce this feature or if any experienced C
> > dev here can work on the implementation of this feature.
> >
> >
> I think this is a much bigger issue than bikeshedding over syntax. The idea
> of typed variables has been discussed a few times in recent years but the
> roadblocks have been efficiency of any possible implementation, BC in
> respect of places type hinting can already be used and the overall
> usefulness / desirability of the feature. It's one of those things that's
> been filled in reasonably well by third party static analysis tools. Tbh
> I'd be more interested in an official static analyzer than more type checks
> at runtime.


It's also worth adding that many times, certain functions cannot be
chained elegantly or the nesting level gets out of hand, making
reusing variables a way to handle that:

$name = implode(' ', array_map(ucfirst(...), $name));

might be more readable to write as:

$name = array_map(ucfirst(...), $name);
$name = implode(' ', $name);

In this case, $name turns from an array to a string. Having to come up
with unique enough variable names just to satisfy type-checking would
be rather annoying (and it is in other languages).

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php



Re: [PHP-DEV] [Discussion] Variable Type Declaration Before Usage

2023-11-05 Thread David Gebler
On Sun, Nov 5, 2023 at 1:37 PM Oladoyinbo Vincent 
wrote:

> Hello Internals,
>
> I was wondering if php could introduce this feature or if any experienced C
> dev here can work on the implementation of this feature.
>
>
I think this is a much bigger issue than bikeshedding over syntax. The idea
of typed variables has been discussed a few times in recent years but the
roadblocks have been efficiency of any possible implementation, BC in
respect of places type hinting can already be used and the overall
usefulness / desirability of the feature. It's one of those things that's
been filled in reasonably well by third party static analysis tools. Tbh
I'd be more interested in an official static analyzer than more type checks
at runtime.