I'm sure I'll just get another canned "LA LA LA LA LA I can't hear you!" response, but oh well.Originally Posted by BerislavLopac
First, let me beat you over the head with the word encspulation. Your object variables should never be exposed... that's just bad practice. There's more than enough literature out there that illustrates why... most importantly, these are difficult to mock out if you're unit testing.
The second thing, it is important that a class conform to an interface if it uses it. Say, for example, I want to have a DataSource interface that describes a common way to access a generic datasource (say, getValue, setValue, etc). Now, I can have classes implement this interface that have their own special methods to do different things. One class can use a database as it's datasource. Another could use XML as a datasource (I bet you're howling now), one could use CSV, another could even use SOAP. Thanks to the contract in place by the interface, classes using the data source do not need to concearn themselves at all over what the data source is... you can happily swap out a DBDataSource and use an XMLDataSource with no problem... because the methods the other object uses to access that data source are present... thanks to the contract.
Now at this time I bet you'll say "well, that's the developers job not to be so retarded to leave those methods out!". Well, what if someone else needs to write a datasource? Let's say they want to use an existing one from something else that doesn't implement the interface? No problem. Just subclass the existing datasource class (or even write an adapter), have it implement the interface, and add the methods in and define yourself how it should access the datasource in a way that any class using the abstract datasource would expect. No worries about "Call to undefined method" errors, no worries about a data source asking for more than it should.
Dependency Injection is a concept that relies quite a bit on interfaces, so making blantant statements like "Making a class conform is useless", while perhaps valid in your case, is rather preposterous.
Only if you're programming by coincidence. I'm reminded of the class on thedailywtf.com that had a variable namned PleaseCompile = true Amongst other programmers, it's good practice to refer to user interfaces as... well, user interfaces. This is the only other interface I can think of besides actual interfaces, which are used in dozens of languages, including php5.Originally Posted by stereofrog