• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1087
  • Last Modified:

What is difference between abstract, interfaces, namespaces?

What is difference between abstract, interfaces, namespaces?

I am learning oop. Please answer with words or a link that compares contrasts all 3. Not looking for individual php.net pages for each.

Abstract child classes can use the same parent constructor

Why would a class use multiple interfaces

Objects can be employee, department from the same namespace file
2 Solutions
Ray PaseurCommented:
You can't really compare namespaces with the other two.  Like the concept of variable scope in functions, the namespace is a way of encapsulating items.  They provide a way in which to group related classes, interfaces, functions and constants.  Unfortunately PHP made the namespace separator into the back-slash which is also the DIRECTORY_SEPARATOR if you're running on a Windows machine.  I guess PHP has a sense of humor and has decided to mess around with Windows users!

Interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.  A class that implements an interface must define all of the methods called for in the interface.

An interface is typically pretty short, since all it does is list the names and argument signatures of the methods it requires.  And in the argument signatures, there will be no code at all.  Instead you will see a semi-colon terminator after the arguments.  You do not instantiate an interface.
public function myMethod($name, $size, $color);

You do not instantiate abstract classes either, you only extend* them.  Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature (name, argument list) - they cannot define the implementation.  The implementation must be defined in the class that extends the abstract class.

The abstract class can have some methods that are not abstract.  So you can think of an abstract class as being a partially formed thing, unusable by itself, but providing structure and, optionally, some of the functionality you need.

* Sorry for the link to PHP4, but it's the best explanation on the site.
To build on what Ray said, it can be hard sometimes to understand the reason WHY you would want to use any of those three items. Often times, you are just thinking about how YOU would build an application. However, those three concepts are vital when you're building applications that will be distributed and used by other people.

Imagine you are a car manufacturer, just like Toyota, Honda, Ford, etc... Each of you has built your own "Car" class. Toyota's "Car" might have different features and abilities than Ford's "Car". Now imagine that you are trying to enter your "Car" into a race against all the others, so you have to submit your "Car" code to a 3rd party. How can you make sure that your "Car" doesn't accidentally end up using the "Tire" class from Honda, or the "SteeringWheel" class from Toyota?

This is where namespaces come in. A namespace is simply a container for your class(es). This way, you can make sure that when your "Car" needs a "Tire", that it is getting the right one. In essence, a namespace is almost like adding a prefix to all your classes:


However, instead of trying to change all of your class names, you simply put them all into the same namespace and they won't get confused or have any conflicts with other classes that have the same name:

namespace RGB192;

class Car

class Tire

class SteeringWheel

... will define RGB192\Car, RGB192Tire, RGB192\SteeringWheel, etc... and you can use that code in an application that also has other classes with the same names.

Interfaces are pretty simple. Just think of interfaces as a list of requirements. Now let's say you are a computer manufacturer. If you built a computer ANY way you wanted, you might end up with something that doesn't turn on because maybe YOU decided that you wanted to make the power plug look like your initials.

In order to make your computer compatible so that other people can plug your computer into the electrical socket in their wall, you are REQUIRED to build your computer in a way that uses a normal power plug that everyone else uses, not one that you built from scratch.

An interface is simply something that says, "When you build an instance of a Foo class, it needs to have X, Y, and Z." You can add more to it, but it needs AT LEAST X, Y, and Z. So an interface is just there to define requirements for other people who want to build their own instances of your class.

An abstract class is like a build-your-own-something toolkit. For example, you can buy "barebones" computer kits out there that provide you with a case, a motherboard, memory, and some other basic parts, but not everything.

It's a starting point, but it's not functional by itself.

The toolkit is meant for you to buy and then fill in with your own hardware (you provide the hard drive, video card, etc...).

This is like an abstract class. The abstract class is there so you can provide some basic functionality to other developers who want to use your code as a starting point. You can build out some of the code so they aren't building everything from scratch, but they're extending your class every time.
rgb192Author Commented:
Latter was more about the comparison which is what I wanted to learn.
Thanks both.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now