[Discussion] Variable Type Declaration Before Usage

From: Date: Sun, 05 Nov 2023 13:37:20 +0000
Subject: [Discussion] Variable Type Declaration Before Usage
Groups: php.internals 
Request: Send a blank email to internals+get-121589@lists.php.net to get a copy of this message
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: ```php 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.

« previous php.internals (#121589) next »