function FormHandler( $prefill = array() ) { /* */ }
I had to Google the "readonly" stipulation. What realistic (or even atypical) case scenarios can you provide that warrants its explicit use? — Outlander
class Person { public function __construct(public readonly string $name) {} } $person = new Person('Michael'); echo $person->name;
class Person { protected $name; public function __construct($name) { $this-name = $name; } public function getName() { return $this->name; } } $person = new Person('Michael'); echo $person->getName();
I'd much rather a default function-level based "error handling" (ie. not integer detected therefore, perform this) than a top level PHP error that breaks whatever the user is doing (and often the site or at least the specific action page in the process).
— Michael
class Person { protected $name; public function __construct($name) { $this-name = $name; } public function getName() { return $this->name; } } $person = new Person('Michael'); echo $person->getName()
class Person { function __construct( $name ) { $this->name = $name; } } $person = new Person( 'Michael' ); echo $person->name;
$person->name = 'Mike';
class Person { public readonly string $initials; public function __construct( public readonly string $first_name, public readonly string $surname, ) { $this->initials = substr($first_name, 0, 1) . substr($surname, 0, 1); } } $person = new Person('John Smith'); echo $person->initials;
Often times you don't want that which is why such properties are usually protected or private. — Michael
$importantInfo = $twilio->importantInfo;
$twilio->importantInfo = 'something that breaks the script';
If they choose to break the code, even unintentionally, perhaps they shouldn't even be coding in the first place. — Outlander
That's the entire purpose of things like public, protected, private, readonly, final, typing, interfaces, abstract, etc; to ensure that developers do things properly. — Michael
Being able to see at a glance 'protected "name"' immediately tells them that they're not supposed to do '$class->name = 'foo'''. — Michael
$class->_reservedVariable; $class->wtf_variable;
That used to be how things were done, but then PHP introduced "protected" and "private" visibility precisely so that we didn't have to do this. — Michael
So it's not like it actually frees up the use of the literal verbose name of the variable itself. It just makes it so "Ok now I have to use use '$name2' or '$variable2' instead of what comes firsthand in mind." as far as secondhand development/utilization of a framework goes. — Outlander
No, it means you're not allowed to change the name. — Michael
Get involved in philosophical discussions about knowledge, truth, language, consciousness, science, politics, religion, logic and mathematics, art, history, and lots more. No ads, no clutter, and very little agreement — just fascinating conversations.