国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP與UML類圖: PHP and UML Class Diagrams

PHP與UML類圖: PHP and UML Class Diagrams

Jun 21, 2016 am 09:14 AM
class function model uml

這篇短文寫的不錯,用PHP來解釋UML(或者反過來說)可以說是一個創(chuàng)新了,通過這篇文章,我也發(fā)現(xiàn)了一個好站: www.phppatterns.com 。各UML術(shù)語的翻譯來源于我正在看的《UML用戶指南(The Unified Modeling Language User Guide)》一書。

UML(Unified Modeling Language, 統(tǒng)一建模語言)是一種通過圖的方式表示軟件的機制。本質(zhì)上它允許我們通過畫圖來設(shè)計我們的程序,并且如果有相應(yīng)的工具,我們甚至可以從圖來直接產(chǎn)生代碼。在這篇文章里,我們將來看看PHP代碼如何通過使用UML類圖(class diagram)的方式表現(xiàn)出來。

我們將直接開始,假設(shè)你已經(jīng)具備了UML的知識,并列舉一些PHP代碼和它們對應(yīng)的UML表示作為例子——但這并不是對類圖的一個完整的分析。

如果你尚未接觸過UML,在你開始閱讀前可以補充一些知識,我們收集了一些資源列在這篇文章的末尾。

[Inheritance 繼承關(guān)系]

PHP關(guān)鍵字extends允許一個類(子類)繼承于另一個類(父類)。

class Senior {

}

class Junior extends Senior {

}
?>


UML方式表示如下:




請注意,三角形在父類這邊。

[Associations 關(guān)聯(lián)關(guān)系]
關(guān)聯(lián)關(guān)系(Associations)發(fā)生于兩個不存在關(guān)系但也許需要互相訪問的類之間,比如Model(模型)和View(視圖),View需要Model提供數(shù)據(jù)以進行顯示。有幾種不同的關(guān)聯(lián)類型:

*Aggregation* 聚合

聚合(Aggregation)是當一個類(下面例子為Model)訪問另一個類(下面例子為Dao)時,第二個類(Dao)也許已經(jīng)被外部實例化($dao)了 。如果第一個對象($model)“掛了”,第二個對象($dao)仍然會繼續(xù)“活著”。這在涉及數(shù)據(jù)訪問對象(data access objects)時很常見,它們也許會被傳遞給很多對象,這些對象就算“掛了”數(shù)據(jù)訪問對象也依然“健在”。

這種方式正常地解釋了第一個類(Model)控制第二個類(Dao)的一部分。

舉個例子:

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


在UML中表示為:




空心菱形在控制類這邊。


*Composition* 組合

組合(Composition)發(fā)生于當于一個類(例子中為View)將另一個類(例子中為LinkWidget)實例化,這樣當前者(View)“掛了”時后者()也跟著“玩完”的情況。

換句話說,第一個類控制了第二個類的全部。
下面是PHP中的一個例子:

class LinkWidget {
function Display () {

}
}

class View {
var $linkWidget;
var $page;
function View () {
$this->linkWidget=new LinkWidget;
}

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


在UML中用下圖表示:




實心菱形在控制類這邊。


[Messages 消息]

消息(Messages)發(fā)生于一個類(例子中為View)同其它類(例子中為HtmlUtils)“交流”而不控制它(HtmlUtils)的實例的時候。這些類之間的關(guān)系同樣是關(guān)聯(lián)關(guān)系(association)。

在PHP中通常發(fā)生于操作符 :: 使用的時候。例如:

譯注: 我覺得這種方式類似于C++中HtmlUtils類中的成員函數(shù)unHtmlEntities()為static的情況,這樣就無需實例化HtmlUtils就可以直接通過"類名::成員函數(shù)名"的方式(HtmlUtils::unHtmlEntities())來調(diào)用了。

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);
}
}
?>



這將表示為:



該消息從View發(fā)給HtmlUtils.(單向發(fā)送)

同樣,消息也可以雙向發(fā)送。

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





[輸出信息: "This is an error message"]

這里 SomeClass 發(fā)送一個消息給 Debug,Debug 訪問了SomeClass 的 $errorMsg 屬性。

[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



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

Trezor Cold Wallet: Model One and Model T Features and Usage Guide Trezor Cold Wallet: Model One and Model T Features and Usage Guide Jan 19, 2024 pm 04:12 PM

After problems occurred in many centralized exchanges, more and more cryptocurrency investors began to transfer assets to cold wallets to reduce the risks posed by centralized exchanges. This article will introduce Trezor, the world's earliest cold wallet provider. Since the first cold wallet was launched in 2014, it has been sold in many countries around the world. Trezor's products include Model One launched in 2014 and the advanced version Model T launched in 2018. The following will continue to introduce the differences between these two products and other cold wallets. What is Trezor cold wallet? In 2014, Trezor launched the first cold wallet ModelOne. In addition to common BTC, ETH, USDT and other currencies, the wallet also supports more than 1,000 other currencies.

How to solve the '[Vue warn]: v-bind:class/ :class' error How to solve the '[Vue warn]: v-bind:class/ :class' error Aug 26, 2023 am 08:17 AM

How to solve the "[Vuewarn]:v-bind:class/:class" error During the development process of using Vue, we often encounter some error prompts. One of the common errors is "[Vuewarn]:v-bind:class" /:class" error. This error message usually appears when we use v-bind:class or :class attribute, indicating that Vue cannot correctly parse the class value we set. Then, if

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

See all articles