taking programming to the next level?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
dajohnson1s
Forum Newbie
Posts: 19
Joined: Sat Nov 15, 2008 12:44 pm

taking programming to the next level?

Post by dajohnson1s »

Hello,

I have been learning php for about 8ish months now, and really enjoy it thus far, but I have noticed that my skills have become somewhat stagnant, and am really not sure where to go to advance my skills. Unfortunately, I tend to follow the structure I would for a University assignment, most were very short (and procedural) and could be solved with a single 'main' function or a few secondary functions, never really touching on OO or anything like that.

I am really into web programming at the moment and have been reading about MVC quite a bit lately, but cannot seem to conceptualize how to make my code into OOP and MVC fashion.

Here is an example page:

Code: Select all

 
<?php
    error_reporting( E_ALL ); //TODO: change to 0 when moving to production server
 
 
    // Sets basic checks
    if( isset( $_GET['mode'] ) && isset( $_GET['type'] ) ){
        $mode = $_GET['mode'];
        $type = $_GET['type'];
    }else{
        header( 'Location: ../start.html' );
        exit();
 
    }
   
    require( 'dbconnection.php' );
   
        $sqlIcon = "
           SELECT
               iconID,
               iconURL
           FROM
               mapItems_icons
       ";
   
    $resultIcon = mysql_query( $sqlIcon );
    if( !$resultIcon )
        die( mysql_error( $conn ) );  //TODO: make a user-friendly error message 
 
   
    if( $mode == 'edit' ){
        //populates the form fields
       
       
        $sql = "
           SELECT
               m.mapItemID,
               m.name,
               m.address,
               m.url,
               i.iconID
           FROM
               mapItems AS m
               INNER JOIN mapItems_icons AS i
                   ON m.iconID = i.iconID
           ORDER BY
               m.name ASC
       ";
 
    }else{
        //populates the drop down
        $sql = "
           SELECT
               iconID,
               iconURL
           FROM
               mapItems_icons
       ";
    }
    $result = mysql_query( $sql );
    if( !$result )
        die( mysql_error( $conn ) );  //TODO: Make error message if query fails
 
    $row = mysql_fetch_array( $result, MYSQL_ASSOC );
    $iconid = $row['iconID'];
   
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Building Information</title>
    </head>
 
    <body>
 
        <form>
            <fieldset>
                <legend>General Building Information</legend>
                <label for="buildingName">Building Name:</label>
                <input id="buildingName" type="text" size="20" value="<?php if($mode == 'edit'){ echo $row['name']; } ?>" />
                <label for="buildingAddress">Building Address:</label>
                <input id="buildingAddress" type="text" size="20" value="<?php if($mode == 'edit'){ echo $row['address']; } ?>" />
                <label for="buildingImage">Image URL:</label>
                <input for="buildingImage" type="text" size="40" value="<?php if($mode == 'edit'){ echo $row['url']; } ?>" />
 
            </fieldset>
            <?php if( $type == 'marker' ){ ?>
            <fieldset>
                <legend>Choose a Marker</legend>
                Select a Marker: <select>
            <?php
                //mysql_data_seek($result, 0);   //resets the array pointer to zero
                while( $row = mysql_fetch_array( $resultIcon, MYSQL_ASSOC ) ){
                    $iconID = $row['iconID'];
                    $iconURL = $row['iconURL'];
                  
                    if( $iconID == $iconid ){
                        echo "<option value=\"$iconID\" selected>{$iconURL}</option>\n";
                    }else{
                        echo "<option value=\"$iconID\">{$iconURL}</option>";
                    }
                }
 
            ?>
                 </select>
            </fieldset>
            <?php } ?>
            <fieldset>
                <legend>Get Coordinates</legend>
                <p>Map will populate here</p>
            </fieldset>
            <br /><br />
            <button>Submit</button>
        </form>
    </body>
</html>
 
With this being my second major php project, you can imagine how much fun this is to maintain later on... :(

Since I am basically just building prototypes and figuring out how I would like things to flow and work, it seems like a great time to try out some new skills. Any advice would be greatly appreciated, I would like some critiques on my code ( I am not proud of this by any means ) and maybe somebody could suggest good materials I could read. I have learned TONS on my own (outside of the classroom) but I am just lost as to where to go and what the next step is after 'the basics'.

Thanks
Daniel
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: taking programming to the next level?

Post by josh »

Check out Zend_Db_Select or the PDO functions for abstracting your SQL. Read TTD development by example by Kent Beck for a way to turn your coding world upside down.

TDDers write lots of little tiny "programs" that automate the tedious job of debugging ( no more playing with values and using var_dump, hardly any more debugging period.. if you stick with it long enough)

Oh yeah and also the Gang of Four book is pretty good. You should learn about patterns like the strategy pattern that epitomize polymorphism, one of the 4 pillars of object oriented design. Design Patterns are like learning about good design, but it is specific advice. Like if you had a shop with multiple payment methods, how do you modularize all the code? Design patterns answer specific questions like these.

For instance if you have a CMS with pages, videos, and pictures, and you make a "comments" system, those comments should work for all content types in your system, if it were well designed. Design patterns help to achieve that level of orthogonality.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: taking programming to the next level?

Post by Christopher »

Download Zend framework and see if you can get it to work. If you can, you are on the next level. ;)
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: taking programming to the next level?

Post by Eran »

+1
nice one, chris :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: taking programming to the next level?

Post by alex.barylski »

+1...me too haha...getting it to work is the next step...thats for sure :)
dajohnson1s
Forum Newbie
Posts: 19
Joined: Sat Nov 15, 2008 12:44 pm

Re: taking programming to the next level?

Post by dajohnson1s »

What makes Zend so bad?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: taking programming to the next level?

Post by Christopher »

I don't think anyone said is was bad. Probably over-engineered is the worst you could say about it. It's a great set of components though.

No, my comment was that if you can build a small app with Zend Framework (or Cake, Symfony, etc. for that matter) that would indicate that you were on the next level. They contain enough current/best practices and standard patterns that simply being functional using one of them is probably a good self litmus test for any programmer who has "been learning php for about 8ish months now, and really enjoy it thus far, but ... never really touching on OO or anything like that."
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: taking programming to the next level?

Post by alex.barylski »

I don't think anyone said is was bad. Probably over-engineered is the worst you could say about it. It's a great set of components though.
I don't use the MVC architecture, but a lot of other components. If you do not have to worry about implementation (which is partially the point of using a framework, IMO) then it is an excellent solution. I would certainly put more faith into Zend than any other open source framework, without question.

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: taking programming to the next level?

Post by josh »

What do you use for MVC? Or do you not MVC?
André D
Forum Commoner
Posts: 55
Joined: Thu Aug 28, 2008 7:03 pm

Re: taking programming to the next level?

Post by André D »

dajohnson1s wrote:I am really into web programming at the moment and have been reading about MVC quite a bit lately, but cannot seem to conceptualize how to make my code into OOP and MVC fashion.
Years ago, before I really learned how to program, I remember having a hard time breaking out of the procedural programming mind set. I knew the HOW of OOP as far as the language syntax, behavior, etc., but I couldn't quite grasp the WHY. I didn't understand why programmers would code with OOP, and I couldn't see what problems OOP addressed that procedural programming couldn't also handle. I read many essays about procedural vs. OO programming, but I didn't get to my "Aha!" moment until I finally read someone's exceptional explanation of polymorphism in OOP. (I wish I could remember the article, but I lost it.)

Of course there are other programming concepts that OOP addresses nicely, but truly understanding polymorphism is what really opened my eyes to the power of OOP. For anyone having trouble getting into OOP, I recommend studying polymorphism specifically.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: taking programming to the next level?

Post by alex.barylski »

What do you use for MVC? Or do you not MVC?
Me???

Yes of course, but in my case, I usually use a much simpler implementation(s) of the fundemental peices that typically make up the boiler plate code of an application. My routers are at most 25 lines, my front controller is less than a 100 lines, action forwarding, pre and post dispatching hooks, etc are implemented separately. I believe I borrowed that idea from Skeleton, however. About the only core functionality to the front controller is invocation of the action and filters. Very simple front controller. :)

Custom dispatching is about the only advantage Zend would have and in my case every application will be implemented using objects, so that is an assumption I can afford to make and not complicate things by introducing yet another layer of abstraction, although even still I think Zend architecture is overly complex.

Cheers,
Alex
Post Reply