Skip to main content

UMLing your PHP project

As I don't have any background on UML, I decided to read about it from scratch. I found at least three tutorials much better of what I could write so I'll be quoting others

What is UML?

Umbrello docs - The Unified Modelling Language (UML) is a diagramming language or notation to specify, visualize and document models of Object Orientated software systems. UML is not a development method, that means it does not tell you what to do first and what to do next or how to design your system, but it helps you to visualize your design and communicate with others. UML is controlled by the Object Management Group (OMG) and is the industry standard for graphically describing software.




PHPKitchen - Why is UML important?

The heart of object-oriented problem solving is the construction of a model. The model abstracts the essential details of the underlying problem from its usually complicated real world. Several modeling tools are wrapped under the heading of the UML, which stands for Unified Modeling Language. The purpose of this course is to present important highlights of the UML.

At the center of the UML are its nine kinds of modeling diagrams, which we describe here.




UML and PHP

PHPPatterns - Now lets jump straight in and assume knowledge of UML and concentrate on some specific PHP code and it's equivalent UML representation - this isn't meant to be a complete analysis of class diagrams.


Inheritance

The extends keyword in PHP allows one class to inherit from another;

[coolcode lang="php"]
< ?php
class Senior {

}

class Junior extends Senior {

}
?>
[/coolcode]
In UML terms this is;

Notice the triangle touches the parent class.

Associations

Associations occur between classes that are not related, but may need access to each other, such as a Model and a View, the View needing the data provided by the Model to rendering the presentation. There are different types of association;


  • Aggregation



  • Aggregation is when one class has access to another, the second class perhaps having been instantiated outside the first. If the first object dies, the second will continue to live. This is commonly seen with data access objects, which may be passed to many other objects, which themselves may die leaving the data access object to continue.

    The way that's normally explained is the first class controls part of the second class.

    For example;
    [coolcode lang="php"]
    < ?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();
    ?>
    [/coolcode]
    In UML that's


    The clear diamond touching the class with control.

  • Composition


  • Composition happens when one class instantiates another, the second class dieing when the first class dies.

    In other words the first class controls the whole of the second class.

    An example in PHP;
    [coolcode lang="php"]
    < ?php
    class LinkWidget {

    }

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

    function renderPage () {
    $this->page=$this->linkWidget->display()
    }
    }
    ?>
    [/coolcode]
    In UML this is represented by;


    A solid diamond touching the class with control.

  • Messages


  • Messages occur where one class communicates with other with out controlling it's instance. These relationship between the classes is also association.

    In PHP that commonly occurs where the :: operator is used. For example;
    [coolcode lang="php"]
    < ?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);
    }
    }
    ?>
    [/coolcode]
    This would be represented as;

    The message is sent from View to HtmlUtils.

    Also messages could be sent both ways, such as;
    [coolcode lang="php"]
    < ?php
    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();
    ?>
    [/coolcode]
    [ Outputs: This is an error message ]

    Here SomeClass sends a message to Debug, which in turn accesses SomeClasses $errorMsg property.




Resources





[tags] object management group, language uml, collaboration diagrams, model abstracts, statechart diagrams, class associations, case diagrams, oriented problem, umbrello, sequence diagrams, unified modelling language, modeling tools, unified modeling language, omg, php code, software systems, inheritance, deployment, scratch, real world[/tags]

Comments

  1. Actually in PHP5 third sample isn't working, you'll recieve message "Using $this when not in object context" on 4 line.

    ReplyDelete
  2. UMLing your PHP project...

    [...]Using UML in a PHP project[...]...

    ReplyDelete
  3. There is one tool for uml
    Sparx Systems - Enterprise Architect

    ReplyDelete

Post a Comment

Popular posts from this blog

اهم التطورات العلمية في العام ٢٠١٩

القضاء: لا دليل على أن مقتحمي الكونغرس خططوا لقتل مشرعين

أكد محققو وزارة العدل الأمريكية أنهم لم يجدوا حتى الآن أي دليل على أن أنصار دونالد ترامب الذين هاجموا مبنى الكونغرس الأسبوع الماضي خططوا لاحتجاز مسؤولين منتخبين وقتلهم. يأتي ذلك في الوقت الذي أُوقفت فيه الشرطة، الجمعة، رجلا مسلّحا في واشنطن خلال محاولته عبور إحدى نقاط التفتيش في محيط مبنى الكونغرس حيث ستقام الأربعاء مراسم تنصيب جو بايدن. في ذات الوقت أفادت شبكة NBC الأمريكية، بأن مكتب التحقيقات الفيدرالي يحقق في إمكانية تمويل حكومات أو جماعات اقتحام مبنى الكونغرس. وقالت مصادر للشبكة: "المكتب يحقق في مدفوعات "بيتكوين" بقيمة 500 ألف دولار، يبدو أنه تم تحويلها من قبل مواطن فرنسي، لشخصيات ومجموعات رئيسية يمينية قبل اندلاع أعمال الشغب". وفي جلسة استماع في محكمة أريزونا بشأن اعتقال أحد مثيري الشغب جاكوب تشانسلي الذي يؤمن بنظرية المؤامرة ومن أتباع الحركة اليمينية المتطرفة "كيو-آنون"، تراجع المدعون الفيدراليون عن اتهامات سابقة بأن أنصار ترامب كانوا يخططون "لاحتجاز مسؤولين منتخبين وقتلهم" في هجوم السادس من كانون الثاني/ يناير في واشنطن. ع
Dear Microsoft : It's over. Our relationship just hasn't been working for a while, and now, this is it. I'm leaving you for another Operating system. I know this isn't a good time--you're down with yet another virus. I do hope you feel better soon--really, I do--but I, too, have to move on with my life. Fact is, in the entire time I've known you, you seem to always have a virus or an occasional worm. You should really see a doctor. That said, I just can't continue with this relationship any longer. I know you say you'll fix things, that next time it'll go better--but that's what you said the last time--and the time before that. Each time I believed you. Well, not any longer. You cheater! The truth is there's nothing more you can say to make things better. I know about your secret marriage to patent. You say you two are not seeing each other anymore, but I just don't believe it. You say you can live without patent, and I've heard that