PHP and UML class diagram: PHPandUMLClassDiagrams

Source: Internet
Author: User
This short article is well written. it can be said that using PHP to explain UML (or conversely) is an innovation. through this article, I also found a good site: www.phppatterns.com. The translation of various UML terms comes from the book I am reading, TheUnifiedModelingLanguageUserGuide. UML (UnifiedModelingLanguage) is a good short article. using PHP to explain UML (or to put it back) can be said to be an innovation. through this article, I also found a good site: www.phppatterns.com. The translation of various UML terms comes from The book "The Unified Modeling Language User Guide" I am reading.

UML (Unified Modeling Language) is a mechanism for expressing software through graphs. Essentially, it allows us to design our program through drawing, and if there is a corresponding tool, we can even directly generate code from the graph. In this article, we will look at how PHP code is presented by using the UML class diagram.

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

If you have not been familiar with UML, you can add some knowledge before you start reading it. we have collected some resources listed at the end of this article.

[Inheritance relationship]

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

Class Senior {

}

Class Junior extends Senior {

}
?>


The UML method is as follows:




Note that the triangle is in the parent class.

[Associations Association]
Associations occur between two classes that do not have a relationship but may need to access each other, such as Model and View. View requires the Model to provide data for display. There are several different Association types:

* Aggregation

Aggregation: when a class (Model in the following example) accesses another class (Dao in the following example), the second class (Dao) may have been instantiated externally ($ dao). If the first object ($ model) is "down", the second object ($ dao) will continue to "live ". This is common when it comes to data access objects, which may be passed to many objects. even if these objects are "hung", the data access objects are still "healthy ".

This method normally explains part of the first class (Model) controlling the second class (Dao.

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


In UML, it is represented:




The hollow diamond is in the control class.


* Composition * combination

Composition occurs when another class (LinkWidget in this example) is instantiated in one class (View in this example), so that when the former (View) is "hung", the latter () it also follows the "playing" situation.

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

Class LinkWidget {
Function Display (){

}
}

Class View {
Var $ linkWidget;
Var $ page;
Function View (){
$ This-> linkWidget = new LinkWidget;
}

Function renderPage (){
$ This-> page = $ this-> linkWidget-> display ()
}
}
?>


Representation in UML:




The solid diamond is in the control class.


[Messages message]

A message occurs when a class (View in this example) "communicates" with other classes (HtmlUtils in this example) and does not control its (HtmlUtils) instances. The relationships between these classes are also association ).

In PHP, the operator: used. For example:

I think this method is similar to the case where the unHtmlEntities () member function in the HtmlUtils class in C ++ is static, so that you can directly use the class name without instantiating HtmlUtils :: the member function name (HtmlUtils: unHtmlEntities () is called.

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 indicates:



Send the message from the View to HtmlUtils. (one-way transmission)

Messages can also be sent in two directions.

Class Debug {
Function display (){
Echo ($ this-> errorMsg );
}
}

Class SomeClass {
Var $ errorMsg = 'This is an error message ';
Function someFunction (){
If (DEBUG = 1 ){
Debug: display ();
}
}
}

Define ('debug', 1 );
$ SomeClass = & new SomeClass;
$ SomeClass-> someFunction ();
?>





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

Here, SomeClass sends a message to Debug. Debug accesses the $ errorMsg attribute of SomeClass.

[Resources]
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 free to use. 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.