-----Ursprüngliche Nachricht-----
Von: Rowan Collins
Gesendet: Mittwoch, 19. September 2018 23:47
An: PHP Internals List
Betreff: Re: [PHP-DEV] [RFC] [VOTE] Typed properties v2At least the approach without nullable properties will lead to a
Throwable when a read is attempted on an uninitialized object, which
is still better than nullability checks all over the place.Is it? Doesn't it just mean writing this:
try {
someFunction($object->propertyThatClaimsToBeNonNullable);
} catch ( TypeError $e ) {
...
}Instead of this:
if ( ! is_null($object->propertyThatClaimsToBeNonNullable) ) {
someFunction($object->propertyThatClaimsToBeNonNullable);
} else {
...
}For that matter, all I need to do is define someFunction as taking a
non-nullable parameter, and I get the TypeError either way.Surely the point of a non-nullable property shouldn't be "it gives a
slightly different error if it's not set", it should be "you don't have
to worry about this not being set, because the language will enforce
that somewhere". (And to cover your last point, that somewhere doesn't
need to be the constructor, if requiring that is really such a big problem.)Regards,
--
Rowan Collins
[IMSoP]
Just an idea: If an object is initialized without having all non-nullable properties initialized, one could store the instantiation place, and give this hint if an uninitialized property is accessed later.
Example:
// User.php
class User {
public Group $group;
}
// functions.php
function initUser() {
$user = new User(); // store this line and filename internally, because User::$group is uninitialized
return $user;
}
// index.php
$user = initUser();
echo $user->group->name; // Throws TypeError informing about access on uninitialized property and that it was initialized in file functions.php at line 3.
The TypeError still occurs but it makes it easy to find and fix the issue. And the same could be done with unsetting - save the place where an non-nullable property has been unsetted and inform the user where it happened if the unsetted property is accessed.
Best regards
Mit freundlichen Grüßen aus Paderborn
Christian Stoller
Web-Entwicklung
LEONEX Internet GmbH
Technologiepark 6
33100 Paderborn
Tel: +49 (5251) 4142-526
Fax: +49 (5251) 4142-501
HRB 8694 AG Paderborn
Geschäftsführer: Stephan Winter
LEONEX ist umgezogen: Bitte beachten Sie unsere neue Adresse sowie unsere neuen Rufnummern.
Wann dürfen wir Sie in unsere neuen Räumlichkeiten einladen?