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.
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.
There is already an RFC on this:
https://wiki.php.net/rfc/local_variable_types, but it doesn't propose a
good usage syntax to promote readability.
Well, i figured out how this feature syntax could look like:
$info: string;
$price: float;
$number: int = 125;
$run: \Closure = fn( string $str ) => $str;
echo $number; // 125
$info = $number; // TypeError
$price = $number; // TypeError
$number = 12.5; // TypeError
$run = "hello world"; // TypeError
I was kind of thinking if the declared variables may also affect functions
params:
function priceInfo($price, $info)
{
return $info . ': $ ' . $price;
}
echo priceInfo(12, 2); // TypeError
echo priceInfo(12.5, 'Price:'); // Price: $12.5
So, what is your opinion on this?
Best Regards,
Oladoyinbo Vincent.
On Sun, Nov 5, 2023 at 1:37 PM Oladoyinbo Vincent oladoyinbov@gmail.com
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.
On Sun, Nov 5, 2023 at 1:37 PM Oladoyinbo Vincent oladoyinbov@gmail.com
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).
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.
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
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 = &TypedInt::$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