Thread: UML for a PHP application?

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question UML for a PHP application?

    For a class I'm doing a web site in PHP for a project (see http://cboard.cprogramming.com/showthread.php?t=74740), and I have to produce a "class diagram."

    We're doing the site procedurally rather than using OO, so how would I go about in creating a class diagram or something similar? Since we're not going to have C-like classes, should we just make each "class" object a page in the site? Or, should we model the real-world objects (e.g., users, airplanes, students, instructor, ...)?

    The instructor didn't give me good feedback, so that's why I posted here.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by ChadJohnson
    For a class I'm doing a web site in PHP for a project (see http://cboard.cprogramming.com/showthread.php?t=74740), and I have to produce a "class diagram."

    We're doing the site procedurally rather than using OO, so how would I go about in creating a class diagram or something similar? Since we're not going to have C-like classes, should we just make each "class" object a page in the site? Or, should we model the real-world objects (e.g., users, airplanes, students, instructor, ...)?

    The instructor didn't give me good feedback, so that's why I posted here.
    You can't do a class diagram if you aren't following an object oriented design. It's really that simple.

    While you might be able to call each "page" a class, it would be pointless. What do they inherit from? What are their methods and properties? Since none of the pages know about each other, it would really be a useless class diagram.

    One way to save your project might be to abstract all the business logic/data access into some PHP classes. You can then make a class diagram from these.

    Edit: You could model each object (users, airlines etc), but since your code doesn't follow that model it's going to be totally inconsitent (and again worthless).

    Why not model the objects like you suggested, then design PHP classes around those objects? You'll find it makes life a lot easier for maintaining your PHP application too.
    Last edited by nickname_changed; 01-26-2006 at 10:51 PM.

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Thanks for the reply.

    I've never done a web site using an OO approach, and I really don't see how this could be done effectively. I mean there is no stucture object-wise for a web page - it's just procedural. Can you explain how a normal web page could be done using an OO approach?

    I think I'm going to just BS it and turn something in but not really follow it in development. He apparently wanted us to do a real program (like a C++ application), but he did not make that very clear (as he never does). I have a requirements document, so I think that's good enough.

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Quote Originally Posted by ChadJohnson
    Can you explain how a normal web page could be done using an OO approach?
    The only time I have used classes in php was when working on an ID3 tag Reader ( where I am currently still working on ... ) , a graphics library ( using the gd2 lib ).

    Guess thats it ... And the only reason I implemented those things as classes and not just seperate php files with some functions in, is because I wanted to try out what this php going OO was all about.

    Looking at a webpage from a OO approach ... I don't have a clue ...

    Maybe you should just ask your teacher how you should implement your solution, does it has to be a program or can it be a webpage using php or something... since obviously both of you are thinking about something completely different.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by ChadJohnson
    For a class I'm doing a web site in PHP for a project (see http://cboard.cprogramming.com/showthread.php?t=74740), and I have to produce a "class diagram."

    We're doing the site procedurally rather than using OO, so how would I go about in creating a class diagram or something similar? Since we're not going to have C-like classes, should we just make each "class" object a page in the site? Or, should we model the real-world objects (e.g., users, airplanes, students, instructor, ...)?

    The instructor didn't give me good feedback, so that's why I posted here.
    If you are going to make a php website, most likely it'll have a data base behind, which must be mapped to a db conceptual model. UML isn't only for programmable classes. You use UML too specify the requisites of the website: user interaction, use cases, actors, permissions. You use UML too to decompose the application both vertically (by use cases and modules) and horizontally (business logic, data layer, user interface, ...). Doing a programming approach with UML is one of the last things to do in these kind of projects.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I've used OOP in creating a PHP site. PHP 4's support of OOP is lacking (haven't spent much time in PHP 5) so its a little harder then say doing OOP in C++. I just got done writting a simple store site and in it my shopping cart is an object that saves the data in the session variable.

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    The actual web pages don't need to be OOP. But think about this.

    Lets say you've got two pages - one that lists all customers in a detailed grid, and one that just lists their names. The customers are stored in a MySQL database.

    In CustomerDetails.php, you probably have:
    Code:
    $q = mysql_query("SELECT * FROM Customers");
    $rows = mysql_fetch_array($q);
    foreach ($row in $rows)
    {
       echo "Name: " + $row["Name"];
       echo "Email: " + $row["Email"];
    }
    And LittleCustomerList.php has this:

    Code:
    $q = mysql_query("SELECT * FROM Customers");
    $rows = mysql_fetch_array($q);
    foreach ($row in $rows)
    {
       echo "Name: " + $row["Name"];
    }
    Notice they do the same thing? That's code duplication. What if someone changes the table "Customers" to "tblCustomers"? You have to go to two files to rename it. Not good (this is an extremely simple, but common, example). What if "Name" changes to "FirstName"/"LastName"? Theres a tone of code that will have to change, and this makes it much harder to maintain.

    So, you can go OOP by doing this (please, forgive me, the syntax might be wrong, but I haven't used PHP for years):

    Code:
    class Customer{
      var $Email;
      var $FullName;
    
      function GetCustomers()
      {
         $results = // new array
    
         $q = mysql_query("SELECT * FROM Customers");
         $rows = mysql_fetch_array($q);
         foreach ($row in $rows)
         {
             $cust = new Customer();
             $cust.Fullname = $row["Name"];
             $cust.Email = $row["Email"];
    
             $results.Add($cust);
         }
    
         return $results;
      }
    }
    Then, your first page becomes:

    Code:
    foreach ($customer in Customer->GetCustomers())
    {
        echo "Name: " + $customer->FullName;
        echo "Email: " + $customer->Email;
    }
    If the table structure changes so that names are stored in two columns ("FirstName"/"LastName" rather than "Name"), you only change your class - nothing else.

    The only thing that knows how to read and write to the customers table is the customer class. This abstraction is what makes OOP great, and and is how you make your applications scale better.

    This is the reason I don't use PHP - PHP is so simple, 99% of the users (yeah, that's a generalization, but shutup) just go around embedding database queries and whatever they like wherever they want. They don't think through design properly. ASP.NET, on the contrast, makes it so much easier to follow a proper object oriented design (not that everyone does, but I think it's more likely).

    Edit: Another thing to think about. What if someday you decide to support two databases - Access and MySQL? Imagine the hundreds of lines of code you'd have to write to do that the first way. Now imagine how much easier it would be doing it the OOP way. Just change the Customers class, and done. OOP makes life good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. combining c php and mysql
    By Thoth in forum C Programming
    Replies: 2
    Last Post: 01-30-2009, 10:55 AM
  2. Any good free UML application?
    By LinuxCoder in forum Linux Programming
    Replies: 8
    Last Post: 04-15-2006, 04:01 AM
  3. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM