Hudzilla.org - the homepage of Paul Hudson
Contents > Objects > Access control modifiers Wish List | Report Bug | About Me ]

6.7.5     Abstract

This is NOT the latest copy of this book; click here for the latest version.

The abstract keyword is used to say that a function or class cannot be created in your program as it stands. This might not make sense at first - after all, why bother defining a class then saying no one can use it?

Well, it is helpful because it does not stop people inheriting from that abstract class to create a new, non-abstract (concrete) class.

Consider this code:

$poppy = new dog;

The code is perfectly legal - we have a class "dog", and we're creating one instance of that and assigning it to $poppy. However, given that we have actual breeds of dog to choose from, what this code actually means is "create a dog with no particular breed". Have you ever seen a dog with no breed? Thought not - even mongrels have breed classifications, which means that a dog without a breed is impossible and should not be allowed.

We can use the abstract keyword to back this up. Here is some code:

abstract class dog {
    private
$Name;
// etc

$poppy = new dog;

The dog class is now abstract, and $poppy is now being created as an abstract dog object. The result? PHP halts execution with a fatal error, "Cannot instantiate abstract class dog".

As mentioned already, you can also use the abstract keyword with functions, but if a class has at least one abstract function the class itself must be declared abstract. Also, you will get errors if you try to provide any code inside an abstract function, which makes this illegal:

abstract class dog {
    abstract function bark() {
        print "Woof!";
    }
}

It even makes this illegal...

abstract class dog {
    abstract function bark() { }
}

Instead, a proper abstract function should look like this:

abstract class dog {
    abstract function bark();
}

Author's Note: If it helps you understand things better, you can think of abstract classes as being quite like interfaces, which are discussed shortly.





<< 6.7.4 Final   6.7.6 Iterating through object variables >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 29 Aug 2008

That confused me too until I reread it. He didn't actually say that you couldn't have a public function inside an abstract class. He said that if you had an abstract function, it had to be empty, and it's class had to be abstract. He should have written that better, so that you know you can have any type of function in an abstract class, too.

capiCrimm@gmail.com - 29 Aug 2008

meh, actually the above isn't legal for syntax reasons, thats what I get for cutting and pasting peices of code together. You should be able to pick the point up though...

capiCrimm@gmail.com - 29 Aug 2008

In reading your article you confused me a bit. An abstract class doesn't need to be a collection of abstract functions. You can have a public function in an abstract class...
exempi graitia..
________________

abstract class legalClass {
protected $value;

public function set_value( $value ) {
return $this->value = $value
}

abstract private function something( $something );
}
________________

The second thing you failed to mention is what purpose an abstract function serves. When you extend the class, the child class MUST provide and implementation of the parents abstract class. To me abstract class is like a promise the parent class made, that all the children will take care of these methods.

my two cents

~capiCrimm



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow