Why does php have interfaces
Last Updated : 25 Jun, Example: php. Previous Traits vs. Interfaces in PHP. Next PHP Interface. Recommended Articles. Article Contributed By :. Samdare B. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. In the following example, we will show you how to use the interface to make the system more flexible and easier to extend. Suppose you have to create a logger that can log a message. The following adds another logger that logs information to the database.
For demonstration purposes, we make the DatabaseLogger class as simple as possible:. And you can easily add other kinds of loggers that implement the Logger interface without touching existing loggers. The following code snippet demonstrates how to use multiple loggers at the same time using a single ILogger interface:.
You could then unit test a part of your code without requiring real servers, file systems, sockets, databases etc. Lots of people will probably hate me for answering this way but the solution to your typing problems can be easily fixed with PHP.
Yes PHP is loosely typed so types are assumed by default, which can cause some problems, especially in comparison operations which is most peoples problem with it. That being said, PHP can be just as strict as any strongly typed language if you cast what you are using into the type you need it to be, and then use bitwise comparison operators.
Here is the easiest example I can think of of what I am saying:. I really just wish developers would stop arguing about these things, if you have a problem with the way PHP works either go program in java and live and let live, or use it in the way that it will do what you want it to What good does bitching about it do for you anyway?
Give you an excuse to screw around all day? Make you look better than someone else? Well it's great that you feel so highly about yourself that you're willing to make someone else look bad but in reality it's your preference and forcing your beliefs on anyone really just makes them code in a way they are not comfortable with causing three things:. It will be the same way changing their methodology or maybe even worse. And maybe it will make you look better in the short term, but the team as a whole will look worse for it remember you may code slower than someone else and that's not necessarily bad as long as the team meets deliverables in a reasonable timeframe, but forcing your habits on someone who has typically performed a little faster may end up making your whole team slow down thus, look worse in a very demanding workflow.
I personally prefer writing procedural PHP code although I can, and have, written full programs using OOP in a few different languages. It really has nothing to do with the practice, but with the habits that you use and even then, a lot of things are my interpreted feelings There are reasons a standard should be established but what you include in the standard you choose is VERY important Thanks for letting me get that off my chest.
Have a great day. Examples: You need to cache your data. There is a lot of different engines for caching, which one is the best? Who cares if you have abstract layer which have some ICacheDriver interface with a set of methods like key, get, put, clear, etc. Just implement to it what you need in current project and change it when you need another.
Or simple usage of toString. You have a set of different showable objects. PHP is used by many entry level programmers , entry level programmers are taught java in college. After their Programming course they start nagging Zend they want java features because that's the way they have been taught to think, an thinking on your own terms or understanding duck typing it's hard, when you're only Zend is pragmatic, it's easier to add the feature other than to pretend they where right all along.
This also buys in more users instead of making them leave, so it must be good. Another instance of this process? People fresh out of. This buys in even more users. And on and on Sign up to join this community. The best answers are voted up and rise to the top.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Why does PHP have interfaces?
Ask Question. Asked 10 years, 7 months ago. Active 1 year, 8 months ago. Viewed 20k times. Improve this question. GSto GSto 8, 7 7 gold badges 37 37 silver badges 59 59 bronze badges. I think the correct question is, why not? HorusKol and before they were implemented, they weren't used, so you can see how they were unused and useless just a version prior. You have to also make and support the claim that their use is somehow an improvement to say that they are useful.
HorusKol Not specious at all.
0コメント