Hi! > Hey all, > > I want to throw out this draft RFC to get the concept floating around and > get feedback early. > > https://wiki.php.net/rfc/protocol_type_hinting
I'm not sure I understand why it is good to have this. This way of checking interfaces is very expensive (since you need to scan the function table) and relies on function names instead of explicit programmer's intent to validate the API (i.e. it assumes if two classes have method named open() this method does the same thing and the classes can be used interchangeably). Since you can easily add "implements" to your class to declare implementation of a specific protocol, why you need less safe and more computationally expensive way? Explicit declaration of interface has value way beyond mere checking function names - it is a contract that you agree to when you write "implements ThisInterface". Absent that, you code starts making assumptions that are very dangerous. Taking the example of the logger here, suppose I make my logger classes work with this API: $log->error(...), $log->info(...), $log->debug(...). Pretty standard way of doing it. This is usually done by __call. Now, when I declare that class implements Logger, I either require __call or just rely on the fact that if you said "implements Logger" then you know what "Logger" means and that it should accept the methods above and do the right thing. However, if I use "protocol typing" (please dispose with "hinting", we really should stop dragging around this unfortunate misnomer) then what would we look for in such class? Would we just accept any class? Any class that implements __call? Also this setup would be rather fragile as absent formal declaration of interface in the implementing class, it is very easy to miss changes in the interface, and since there's no explicit link between implementing class and the interface, no tool can warn you about that change in the interface until you try to use the object and it starts failing. With explicit declaration any good IDE will tell you your implementation is missing a method, and even missing that you'd be told about the class no longer being good immediately on loading, not when you try to use it. Also note that unlike the interface check, this check would also force loading the checked interface (since you can't check the methods without having it loaded) which provides additional performance drag. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php