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

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

10 things Dorothée Loorbach learned after losing a lot of money

Dorothée isn't just sharing her life changing experience with work and money, and sharing the following tips which won't make much sense without listening to the tips in her own words Money is important Money equals time Money equals value What people say doesn't matter What people say matters most when people is you! It's really simple - spend less, earn more, invest wisely and value yourself. It's not that easy Being broke sucks Stay Broke - be present in your own life Money isn't important https://youtu.be/_8l2egORXGA

Rules of war (in a nutshell)

https://youtu.be/HwpzzAefx9M Since the beginning, humans have resorted to violence as a way to settle disagreements. Yet through the ages, people from around the world have tried to limit the brutality of war. It was this humanitarian spirit that led to the First Geneva Convention of 1864, and to the birth of modern International Humanitarian Law. Setting the basic limits on how wars can be fought, these universal laws of war protect those not fighting, as well as those no longer able to. To do this, a distinction must always be made between who or what may be attacked, and who or what must be spared and protected.