Abstract Classes in PHP: Working with PHP 5

Here we are again. This is the tutorial you’ve been waiting for! Welcome to the last part of the series “Abstract classes in PHP.” If you’ve already read the previous articles, then you’ll know that this series introduces the basics of abstract classes in PHP 4/PHP 5, and illustrates their use and implementation in concrete situations.

Introduction

Before I leap forward, it’s worth a brief look back to remind you of what you learned during the previous installment of this series, so it’ll be much easier to continue with the topics that will be discussed in this final tutorial. As you’ll recall, in the second part I demonstrated how to create a well-defined hierarchy of data processing PHP 4 classes, with the base class being abstract. This originally provided a set of undefined, highly generic methods for converting data to different formats.

In consonance with the concepts deployed before, I derived two specific subclasses from the base class. These classes were responsible for processing file data, along with MySQL result sets. To achieve these tasks, each child class provided a concrete implementation for each generic method declared within the corresponding parent class. This condition demonstrated how to use a PHP 4 abstract class as the starting point from which to derive as many subclasses as needed, in order to perform concrete operations within a given PHP application.

As you know, the whole idea of defining an abstract class is to allow you to define generic characteristics of a given object (including properties and methods), and then implement a concrete functionality by using the methods of the respective subclasses. So now it’s clear and fresh in your mind what abstract classes are and how to use them during the development cycle of a Web application.

That said, in this last part of the series, I’ll explain the key points of abstract classes in PHP 5, and additionally provide you with some hands-on examples. This should give you a clear idea of how to utilize them within the powerful Object Model implemented in the latest version of PHP.

Assuming that you’re interested in learning more about PHP 5 abstract classes, it’s time to move forward and find out how to use them. Let’s get going!

{mospagebreak title=Working with a highly improved object model: defining abstract classes in PHP 5}

Because of the numerous improvements introduced in its Object Model, PHP 5 makes it much easier to work with abstract classes. The process is as simple as preceding the “class” keyword with the term “abstract” and following certain prerequisites that should be met (more about this in a moment). Of course, as with PHP 4, abstract classes in PHP 5 cannot be instantiated, and any attempt to instantiate them will result in a fatal error triggered by the PHP interpreter.

Right, that’s the explanation of the theory. Now, let me show you a regular PHP 5 class and next, allow me to demonstrate how I can turn it into an abstract one. Look at the following example:

class Message{
    private $message;
    public function __construct($message){
        if(!is_string($message)){
            throw new Exception(‘Invalid parameter type!’);
        }
        $this->message=$message;
    }
    public function fetchMessage(){
        return $this->message;
    }
}
try{
    // instantiate ‘$message’ object
    $message=new Message(‘This is a regular PHP 5 class.’);
    echo $message->fetchMessage();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

As you can see, this isn’t rocket science. What I’ve done above is simply define a regular PHP 5 class, which only displays string messages, passed on as a parameter to its constructor. So far, it’s nothing unexpected, so I’ll go one step further and turn this class into an abstract one. Take a look:

abstract class Message{
    private $message;
    public function __construct($message){
        if(!is_string($message)){
            throw new Exception(‘Invalid parameter type!’);
        }
        $this->message=$message;
    }
    public function fetchMessage(){
        return $this->message;
    }
}
try{
    // try to instantiate ‘$message’ object – throws a fatal
error
    $message=new Message(‘This is an abstract PHP 5 class.’);
    echo $message->fetchMessage();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

 

Here, there are a few interesting things worth noting. First, if you run the above script, you’ll be faced with a fatal error. Since the sample class is now declared abstract, it’s not possible to perform its instantiation, a fact the PHP parser will let you know by raising a fatal error and halting the script.

Of course, the second thing to notice is the simple inclusion of the “abstract” keyword, in order to indicate to the interpreter what type of class I intend to work with. In this case, I’ve kept the class code intact, so you can easily understand the whole definition process, but it’s rather pointless and a bad programming habit to provide specific implementation for all the class methods.

In addition, there are some rules that must be followed, which surely will make your life easier when working with abstract classes in PHP 5. First, if you define an abstract method inside any class, the containing class must be abstract as well. Otherwise, a fatal error will be triggered.

Second, according to what you learned in my previous article, abstract methods only declare the signature; they do not provide explicit implementation. And finally, if you derive a subclass that implements an abstract method, the visibility of this method must be the same or weaker than the one specified in the parent class. Maybe this sounds like a convoluted thing, but don’t feel concerned right now. At the moment of coding the corresponding examples, the above rules should become clear to you.

Fine, now you know how to declare an abstract class in PHP 5. However, perhaps you’re wondering: is there any way to call the methods of an abstract class, without getting a fatal error? Well, even when this isn’t actually a desirable thing, there are times when you need to call a method out of the object context, by using the (::) scope resolution operator. To find out how this is done, please read the following section.

{mospagebreak title=Calling class methods out of the object context: using the scope resolution operator}

As I explained before, you can declare a class abstract, and possibly even call their methods without raising a fatal error. Of course, this process is rather pointless and unusual, but for the sake of completeness, here’s how I’d call the methods of the sample abstract class you saw right at the beginning of the article, by using the scope resolution operator:

abstract class Message{
    private static $message;
    public function setMessage($message){
        if(!is_string($message)){
            throw new Exception(‘Invalid parameter type!’);
        }
        self::$message=$message;
    }
    public function fetchMessage(){
        return self::$message;
    }
}
try{
    // call ‘setMessage()’ method out of object context
    Message::setMessage(‘This is an abstract PHP 5 class, and its
methods are called out of the object context.’);
    echo Message::fetchMessage();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

As you can see, the above example uses the double colon operator (also called Paamayim Nekudotayim, or double-colon in Hebrew), in order to call the class methods without raising a fatal error. However, in most cases abstract classes must include abstract methods, which turns the above snippet into a rather inefficient method for using an abstract class, since all its methods are explicitly implemented.

At this stage, I hope you understand how to define an abstract class in PHP 5. However, all the theory that you just learned is rather useless if I don’t show you a concrete case. It’s precisely for this reason that over the next few lines, I’ll set up an illustrative example. Thus you can grasp the concepts for using abstract classes in PHP 5. Click the link and keep reading.

{mospagebreak title=Using abstract classes in PHP 5: setting up an example}

The thing about abstract classes, though, is that all their related concepts are grasped better by studying some sample code. If you’re anything like me, you may want to see how an abstract class can be used in a more useful situation. Right, I’ll start developing an example, by defining a base abstract class named “dataSaver” (yeah, sometimes my creativity blows me away), which will reside on top of the hierarchy of classes, and expose an abstract “save()” method, useful for modeling generic behaviors for all “dataSaver” objects. Please, have a look at the signature of this class:

abstract class dataSaver{
    protected $data;
    protected function setData($data){
        $this->data=$data;
    }
    // abstract ‘save()’ method
    abstract protected function save();
}

While the definition for the above class looks very straightforward, there are certain things worth highlighting. Aside from declaring this class abstract, notice the “protected” visibility assigned to class methods, as well as to the $data property. This means that class members can only be accessed from the originating class or from further child classes. See how I’m restricting the implementation of abstract methods only to the subclasses? I hope you do.

Now the rule mentioned before should be clear: since the base abstract “dataSaver” class defines also an abstract method, not only must all its subclasses provide implementation for it, but its visibility must also be the same or weaker, which implies having to declare this method at least protected or public.

Having defined the base “dataSaver” class, let’s leap forward and define a couple of subclasses, which will explicitly implement the abstract “save()” method. Here’s the definition for the first child class:

class fileDataSaver extends dataSaver{
    private $file;
    public function __construct($data,$file){
        // call parent method ‘setData()’
        parent::setData($data);
        $this->file=$file;
    }
    // save data to file
    public function save(){
        if(!$fp=fopen($this->file.’.txt’,’a’)){
            throw new Exception(‘Error opening target file’);
        }
        fwrite($fp,$this->data);
        fclose($fp);
    }
}

In this case, the “fileDataSaver” subclass provides implementation for the abstract “save()” method, and uses it for saving the data passed on to the constructor to a specified target file. Notice how this method has a weaker visibility (declared public) than the one assigned in the parent class.

Now, let’s extend the current example and derive another subclass from “dataSaver.” This comes in quite useful for seeing a different implementation for the abstract “save()” method. This brand new child class is defined as follows:

class cookieDataSaver extends dataSaver{
    private $cookie;
    public function __construct($data,$cookie){
        // call parent method ‘setData()’
        parent::setData($data);
        $this->cookie=$cookie;
    }
    // save data to cookie
    public function save(){
        setcookie($this->cookie,$this->data);
    }
}

 

This class is fairly simple, but worth examining in detail, since it defines a new implementation for the “save()” method. In this case, the above class stores the content of the incoming $data parameter in a simple cookie, and additionally illustrates how to assign the correct visibility for the method in question.

Finally, in case you’re wondering, here is the complete list of defined classes, conjunctly with the instantiation of both “fileDataSaver” and “cookieDataSaver” objects:

// define abstract base class ‘dataSaver’
abstract class dataSaver{
    protected $data;
    protected function setData($data){
        $this->data=$data;
    }
    // abstract ‘save()’ method
    abstract protected function save();
}
// define concrete class ‘fileDataSaver’
class fileDataSaver extends dataSaver{
    private $file;
    public function __construct($data,$file){
        // call parent method ‘setData()’
        parent::setData($data);
        $this->file=$file;
    }
    // save data to file
    public function save(){
        if(!$fp=fopen($this->file.’.txt’,’a’)){
            throw new Exception(‘Error opening target file’);
        }
        fwrite($fp,$this->data);
        fclose($fp);
    }
}
// define concrete class ‘cookieDataSaver’
class cookieDataSaver extends dataSaver{
    private $cookie;
    public function __construct($data,$cookie){
        // call parent method ‘setData()’
        parent::setData($data);
        $this->cookie=$cookie;
    }
    // save data to cookie
    public function save(){
        setcookie($this->cookie,$this->data);
    }
}
try{
    // instantiate ‘fileDataSaver’ object
    $fileSaver=new fileDataSaver(‘This is file data’.”n”,’datafile’);
    // save data to file
    $fileSaver->save();
    // instantiate ‘cookieDataSaver’ object
    $cookieSaver=new cookieDataSaver(‘This is cookie
data’,’testcookie’);
    // save data to cookie
    $cookieSaver->save();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

At this point, I’ve shown you a concrete example of how to create and use an abstract class in PHP 5, by building a hierarchy of classes, where the base class defines generic object methods, and the respective subclasses provide a specific implementation for each of them. Even when the sample codes that I wrote here are pretty rudimentary, they can be used as the foundation for writing more complex applications. As you know, after digesting the appropriate theory, practice is what you’ll need most in these cases.

Bottom line

In this series, I hope you’ve expanded your grounding in object-oriented PHP, by understanding the creation and application of abstract classes. If you’re an experienced Web developer working on a website or application, you might find these articles quite useful in your next development effort. Otherwise, if you’re a beginning programmer, wanting to know how abstract classes fit into the OOP schema, I provided you with some handy illustrations for starting quickly on the subject. See you in the next tutorial!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye