note 124366 added to language.oop5.visibility
| From: | Patanjali@php.net | Date: | Mon, 04 Nov 2019 22:19:43 +0000 |
| Subject: | note 124366 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212884@lists.php.net to get a copy of this message | ||
Re: wbcarts at juno dot com
Expected, since it comes down to PHP's lack of type enforcement.
Classes preserve structure, but doesn't natively enforce scalar property types, as per
PHP's variables.
'It REQUIRES OUTSIDE CODE to do all the work...'
No, as you indicate, to mitigate this, make variables private and use protected/public
'set' and 'get' types of methods to manipulate their values.
However, the 'set' functions would also need to enforce type and report errors if not of
the correct type.
Of course, the code that uses classes should be designed to either:
a. Provide only the correct types of values.
b. Handle the errors reported.
c. Encapsulate use of the class with try-catch blocks.
Note that c. is what sloppy code feeding mistyped values to the class would have to do if type were
strictly enforced.
Fortunately, as of 7.0, type can be specified for scalar function/method parameters, but would need
try-catch blocks to handle mismatches from inside the class, rather than using the 'is'
functions.
All of this is part of proper design and defensive programming.
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.54 (proxied: 202.7.222.4)
----
Manual Page -- http://php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/124366
Del: integrated -- https://master.php.net/note/delete/124366/integrated
Del: useless -- https://master.php.net/note/delete/124366/useless
Del: bad code -- https://master.php.net/note/delete/124366/bad+code
Del: spam -- https://master.php.net/note/delete/124366/spam
Del: non-english -- https://master.php.net/note/delete/124366/non-english
Del: in docs -- https://master.php.net/note/delete/124366/in+docs
Del: other reasons-- https://master.php.net/note/delete/124366
Reject -- https://master.php.net/note/reject/124366
Search -- https://master.php.net/manage/user-notes.php