Solved

What is difference between abstract, interfaces, namespaces?

Posted on 2014-01-19
3
866 Views
Last Modified: 2014-01-23
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
0
Comment
Question by:rgb192
3 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39792551
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!
http://www.php.net/manual/en/language.namespaces.rationale.php
http://www.php.net/manual/en/language.namespaces.basics.php
http://www.php.net/manual/en/language.namespaces.faq.php

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);
http://www.php.net/manual/en/language.oop5.interfaces.php

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.
http://www.php.net/manual/en/language.oop5.abstract.php

* Sorry for the link to PHP4, but it's the best explanation on the site.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 39792664
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.

NAMESPACES:
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:

RGB192_Car
RGB192_Tire
RGB192_SteeringWheel
...etc....

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:
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.

ABSTRACT CLASSES:
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.
0
 

Author Closing Comment

by:rgb192
ID: 39805680
Latter was more about the comparison which is what I wanted to learn.
Thanks both.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now