Re: function overloading

From: Date: Tue, 22 Nov 2016 04:00:03 +0000
Subject: Re: function overloading
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to internals+get-97114@lists.php.net to get a copy of this message
Hi! > a typical use case would be > > class EventHandler { > public function handle(ProductCreated $event) { > ... > } > public function handle(ProductNameChanged $event) { > ... > } > } > > vs what's currently being done: > > class EventHandler { > public function handleProductCreated(ProductCreated $event) { > ... > } > public function handleProductNameChanged(ProductNameChanged $event) { > ... > } > } And that's exactly why we shouldn't do it. Having a class with 20 methods called "handle" is a nightmare. Especially when you try to figure out which of those handles this line of code is calling. In Java, you'd at least have total typing to help you (and even then I'd recommend against this (ab)use of overloading), in PHP, you may not have that help. Google C++ style guide says: Use overloaded functions (including constructors) only if a reader looking at a call site can get a good idea of what is happening without having to first figure out exactly which overload is being called. -- Stas Malyshev smalyshev@gmail.com

« previous php.internals (#97114) next »