PHP and UML class diagrams: phpandumlclassdiagrams_php

Source: Internet
Author: User
Keywords UML one class function example Vie
This essay is good, the use of PHP to explain the UML (or conversely) can be said to be an innovation, through this article, I also found a good station: www.phppatterns.com. The translation of the UML terms comes from the Unified Modeling Language User Guide, which I am looking at.

UML (Unified Modeling Language, Unified Modeling Language) is a mechanism for representing software by means of graphs. Essentially it allows us to design our programs by drawing, and if we have the tools, we can even generate the code directly from the graph. In this article, we will look at how the PHP code is represented by the use of the UML class diagram (the class diagram).

We will start directly, assuming that you have the knowledge of UML and enumerate some PHP code and their corresponding UML representations as examples-but this is not a complete analysis of the class diagram.

If you have not yet contacted UML, you can add some knowledge before you start reading, and we have collected some resources at the end of this article.

[Inheritance inheritance relationship]

The PHP keyword extends allows one class (subclass) to inherit from another class (the parent Class).

Class Senior {

}

Class Junior extends Senior {

}
?>


The UML approach is represented as follows:




Notice that the triangle is on this side of the parent class.

[Associations Relation]
The Association relationship (associations) occurs between two classes that do not exist but may need to be accessed from each other, such as model (models) and view (view), and view requires model to provide data for display. There are several different types of associations:

*aggregation* Aggregation

Aggregation (Aggregation) is when a class (the following example is model) accesses another class (the following example is DAO), the Second Class (DAO) may have been externally instantiated ($dao). If the first object ($model) is "hung", the second object ($dao) will still remain alive. This is common when it comes to data access objects, which may be passed to many objects, which are still "alive" even if the data access object is "hung".

This approach normally explains part of the first Class (Model) to control the second class (Dao).

As an example:

Class Dao {
function getsomething () {

}
}

Class Model {
var $dao;
Function Model (& $dao) {
$this->dao=& $dao;
}

function dosomething () {
$this->dao->getsomething ();
}
}

$dao =new DAO;

$model =new model ($DAO);
$model->dosomething ();
?>


expressed in UML as:




The Hollow diamond is on the control class side.


*composition* Combination

A combination (composition) occurs when a class (in the case of view) instantiates another class (in the example, Linkwidget) so that the current (view) "Hangs" when the latter () also follows the "Finish" case.

In other words, the first class controls all of the second class.
Here is an example of PHP:

Class Linkwidget {
function Display () {

}
}

Class View {
var $linkWidget;
var $page;
function View () {
$this->linkwidget=new Linkwidget;
}

function Renderpage () {
$this->page= $this->linkwidget->display ()
}
}
?>


expressed in UML:




The solid diamond is on the control class side.


[Messages message]

The message (Messages) occurs when a class (in the example, view) "communicates" with other classes (htmlutils in the example) without controlling an instance of it (htmlutils). The relationship between these classes is also associated with the Relationship (association).

In PHP, it usually happens in the operator:: When used. For example:

I think this approach is similar to the case where the member function unhtmlentities () in the Htmlutils class in C + + is static, so that you do not have to instantiate htmlutils directly through the "class name:: member function name" Method (Htmlutils:: Unhtmlentities ()) to invoke.

Class Htmlutils {
function Unhtmlentities ($STR) {
$trans _tbl = get_html_translation_table (html_entities);
$trans _tbl = Array_flip ($trans _tbl);
Return Strtr ($STR, $trans _tbl);
}
}

Class View {
function Renderpage {
$text =htmlutils::unhtmlentities ($text);
}
}
?>



This will be expressed as:



The message is sent from view to Htmlutils. (One-way delivery)

Similarly, messages can be sent in both directions.

Class Debug {
function display () {
Echo ($this->errormsg);
}
}

Class SomeClass {
var $errorMsg = ' This was an error message ';
function SomeFunction () {
if (DEBUG = = 1) {
Debug::d isplay ();
}
}
}

Define (' DEBUG ', 1);
$someClass = &new SomeClass;
$someClass->somefunction ();
?>





[Output information: "This is an error message"]

Here SomeClass sends a message to Debug,debug to access the $ERRORMSG property of SomeClass.

[Resources Resource]
Introduction to UML from the Object Management Group
Posideon uml-a Tool for drawing UML diagrams and generating Java (sadly no PHP), the Community edition being . Based on Argo UML, an open source project.
Object Mentor on UML
A UML Reference Card
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.