PHP and UML classes diagram: PHP and UML class diagrams

Source: Internet
Author: User
Tags object inheritance php and php code access
This essay is well written, in PHP to explain the UML (or vice versa) 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 UML User Guide (the Unified Modeling Language user Guide) that I am looking at.

UML (Unified modeling Language, Unified Modeling Language) is a mechanism for representing software in the form of graphs. In essence it allows us to design our programs by drawing, and if we have the tools, we can even generate code directly from the graph. In this article, we'll look at how PHP code behaves through the use of UML class diagrams (class diagram).

We'll go straight to the assumption that you already have UML knowledge and enumerate some of the 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 begin to read, and we have collected some resources at the end of this article.

[Inheritance inheritance relationship]

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

<?php
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 Association Relationship]
The Association relationship (associations) occurs between two unrelated classes that may need to be accessed, such as model and view, and view needs model to provide data for display. There are several different types of associations:

*aggregation* polymerization

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

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

As an example:

<?php
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 ();
?>


Represented in UML as:




The Hollow diamond is on the control class side.


*composition* Combination

A combination (composition) occurs when a class (view in the example) instantiates another class (the example of linkwidget) so that the latter () is "dead" when the current person is "hung".

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

<?php
Class Linkwidget {
function Display () {

}
}

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

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


In UML, the following figure is represented:




The solid diamond is on the control class side.


[Messages message]

A message (Messages) occurs when a class (view in the example) is "communicated" with other classes (htmlutils in the example) without controlling the instance of it (htmlutils). The relationship between these classes is also related to the Relationship (association).

In PHP, it usually occurs 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 no instantiation htmlutils can be passed directly through the "class name:: member function name" (Htmlutils:: Unhtmlentities ()) to invoke the.

<?php
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 to htmlutils from view. (One-way send)

Similarly, messages can be sent in both directions.

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

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

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





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

Here SomeClass sends a message to Debug,debug to access the SomeClass's $ERRORMSG properties.

[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 . Based on Argo UML, an open source project.
Object Mentor on UML
A UML Reference Card

Related Article

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.