Re: Explicit constructor call and polymorphic dispatch
| From: | Andreas Heigl | Date: | Sat, 14 Jan 2017 12:15:25 +0000 |
| Subject: | Re: Explicit constructor call and polymorphic dispatch | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-97751@lists.php.net to get a copy of this message | ||
> Am 14.01.2017 um 11:26 schrieb Fleshgrinder <php@fleshgrinder.com>:
>
>> On 1/14/2017 11:20 AM, Andreas Heigl wrote:
>> Do we as "makers of PHP" want to dictate the user what the "intended"
>> behaviour of PHP is? Or are we treating the user as a responsible person
>> that knows what to do and also not to do?
>>
>> Personally I'm in favour of the later. Let the user be able to call
>> __construct whenever they like. It's a special method which is clearly
>> visible by the two underscores at the start. So the users either know
>> exactly what they are doing or they need to learn it.
>>
>> Translated to a different example this discussion is like: "Let's remove
>> the handle from the window because someone might be able to open the
>> window and jump out of it". The handle is there for a valid reason. And
>> as a window-maker I have no idea in what way it might be used. But I
>> have to accept that there are people missusing it. But should I limit
>> all those users with a valid use-case for it just to save some from
>> missusing it? And if we start with that, where should we stop?
>>
>> Just my 0.02€
>>
>> Cheers
>>
>> Andreas
>>
>
> Yes, that is exactly the question. This needs to be voted on. Rust would
> be an example that protects you and C/C++ would be examples that do not.
> Both approaches are valid approaches.
>
> Note that the dictation will not always work, this is why e.g. Rust has
> unsafe blocks. The corresponding thing in PHP imho would be reflection.
> Hence, if someone has a special case:
>
> - Rust: use unsafe
> - PHP: use reflection
>
> --
> Richard "Fleshgrinder" Fussenegger
IMHO it already IS safe.
In PHP 4 the "constructor" was a method thad had the same name as the class. But now it is
a class that has always the same name and is always preceded with two underscores. There is no way
to "accidentaly" call that method. You have to explicitly call "__construct" to
"break" things.
If developers "want" to break something they will find a way. Protecting
"__construct" will not stop them from doing so. But it will stop the rest of us do
legitimate things.
Cheers
Andreas