php abstract class real example

Please give real world explanations of why a php abstract class is needed.

What are the limitations.
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
This topic has been fairly extensively covered - see links below

An abstract class is one that defines methods but does not necessarily implement them. You are forced to implement the methods in derived classes. Similar to interfaces but allow you to define code in the base class.

See here for more info
The PHP Manual on Abstraction
http://php.net/manual/en/language.oop5.abstract.php

When to use Abstract vs Interface
http://stackoverflow.com/questions/1814821/interface-or-an-abstract-class-which-one-to-use

Advantages of using an Abstract class
http://stackoverflow.com/questions/6373224/what-are-the-advantage-if-i-use-abstract-class-in-php

Some more discussion on the topic
http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html
0
Ray PaseurCommented:
This comment is pretty "on point."
http://www.php.net/manual/en/language.oop5.abstract.php#82111

The concepts of abstract and interface are used when there are several engineers working on a project.  The goal of "tightening up" the code among many co-workers is very simple: we want to prevent run-time errors by causing compile-time errors that can be fixed before the application is deployed.  

In a way it's like using error_reporting(E_ALL).  To a novice, this is annoying because it shows all of his errors.  To a professional this is revealing because it enables him to fix all of his errors.

An abstract class is a partial class definition that must be completed (maybe by another engineer) before it can be used to make an object.  Why use an abstract class?  Why not just tell the other engineer that he has to extend your base class?  Because we are human and forget things!  Because the engineers may be in Ireland, California, India and New Zealand.  By using the abstract class, we can forget, but PHP will remind us and save us from run-time errors.

An abstract class cannot be instantiated.  It can only be extended.  The extensions can be instantiated.  The extensions must define all of the abstract functions that are identified in the abstract class definition.  In an abstract class, the functions that are not abstract may be written out and be complete.  Think of an abstract class that defines a  house.  The foundation() method is complete (not abstract) inside the class, but the walls() and roof() are abstract methods.  This would require anyone who extends the house class to provide walls() and roof(). Thus it prevents the run-time instantiation of a useless house object.  You might extend the house class to create either a Pagoda or a Colonial.  The Pagoda or the Colonial class would be the underlying class instance, so this is what you might build on top of the abstract class house.

Another example is the idea of a "person" who could be either a "student" or "employee" with shared humanity but different roles in life.  While we carry information about the person, it is not information that is useful or visible except through the role of student or employee. See this example.
http://www.php.net/manual/en/language.oop5.abstract.php#83582

Compare the interface.
http://www.php.net/manual/en/language.oop5.interfaces.php#107364

The interface simply tells the developer what methods must be implemented.  It does not provide any of the methods.  Thus it becomes something of a "contract" between developers.  Perhaps the lead developer will create the interface and then assign each programmer a responsibility for an implementation of the methods. One programmer is an expert at MySQL, another is an expert at MySQLi, another is an expert at PostGreSQL, etc.  When it comes time to test, if any of the developers is missing one of the methods in the class that implements the interface, PHP will catch the omission at compile-time, thus helping us to prevent a run-time error.

You can create an object instance of an interface by using the keyword "new" in the same way that you can create an object instance of a class.  Then you can call the methods of the interface, and you will be running the methods that are defined in the implementation of the interface.

At some point in this discussion you will find someone talking about "multiple inheritance."  When that nonsense starts, get up and walk out of the room!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
greetings  rgb192, , you again ask about using the PHP Classes, as if you want to learn about the ways and advantages of PHP Classes and your question of -  "Please give real world explanations of why a php abstract class is needed."
My opinion of the "is the php abstract class is needed?" is an absolute "NO IT IS NOT NEEDED! ! !"
so do not even try and learn or use this PHP abstract class!
It may be possible in some advanced multi-developer setup, where there are 20 or more developers workin on an advanced implementation of a very complex multiple CLASS, where this php abstract class could be useful, but if the developers have a normal communication with each other, then no it is not useful, my opinion.

a note to try and help you in learning and more importantly DEFINING and USING your own PHP Classes, if you read something in the  use of PHP CLASSES that you KNOW will help you in setting up your PHP CLASS, then continue to read and learn about that method or property of a PHP CLASS, but  try and learn about things that you UNDERSTAND first, , before you try anything that is so far above your learning like this  abstract class thing!
0
rgb192Author Commented:
An abstract class is one that defines methods but does not necessarily implement them. You are forced to implement the methods in derived classes. Similar to interfaces but allow you to define code in the base class.

I like this best:
In a way it's like using error_reporting(E_ALL).  To a novice, this is annoying because it shows all of his errors.  To a professional this is revealing because it enables him to fix all of his errors.

where there are 20 or more developers workin on an advanced implementation of a very complex multiple CLASS


thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.