What would be a suitable design pattern for use in this structure of the PHP class, without considering multiple inheritance?

I have an abstract class called Node. It contains a constructor that takes a string from my database and creates basic information. All pieces of content on my site extend this class - Person, Event, Projectetc.

The 3 of these expandable classes are special - when they are created, in addition to pulling values ​​from the database, they must also request a web service; if the web service provides values ​​other than the values ​​given in the database, they must be stored in the database.

In a language with multiple inheritance, this will be quite simple; any of these classes will extend both Node, and so APIData, or something like that. Without IM, I'm not sure how to handle this. Using an interface would not help, as it does not provide concrete implementations.

A decorator sample is sometimes recommended as a replacement for some IM functions, but I have not enough experience to determine if this is a suitable choice. Any suggestions?

+5
source share
3 answers

Since the class APIDatawill use the functionality from your class Node, you should simply extend it. Here are a few pseudo codes:

abstract class APIData extends Node {

    public function __construct($data) {
        parent::__construct($data);
        $this->checkData();
    }

    protected function checkData() {
        // load data from webservice
        $data = $this->loadData();

        // check if data is the same
        foreach($data as $item => $value) {
            if ($this->data[$item] != $value) {
                // save in database
            }
        }
    }
}
+1
source

. - , , , , .. . - . , .

, , .

, - , , PersonService, EventService .., :

  • webservice:
    • webservice
    • ,
  • contructor

webservice , , , , (EDIT: DAO, ).

+3

, ", ". , , . , , , , , .

, , . , Node APIData -, , Node APIData, .

When PHP 5.4 comes out with its own features, it will be much easier.

0
source

All Articles