I've been coding for a few years now, but never got the hang of OOP style coding which everyone who is hiring seems to want these days. I have an interview tomorrow and tried to put together a little portfolio today and this is what I came up with:

www.chrisadkins.me

Please tell me if you think my OOP style coding is good enough to get a job that is looking for a PHP coder with only 3 years experience. I've been coding with PHP since 2007 but feel like I've never mastered it the way some of you have.

Thanks for any advice whatsoever!

    It's not very "object-oriented" - it's just couple functions wrapped in a class. An "object" should represent a thing (an IP address, in your example) and do particular things to it (count how many times you've seen it before). This should all happen internally, so your code doesn't need to worry about how it's done.

    Another suggestion would be to create the DB connection outside the IP object (it's a different "thing," after all), and pass it to your IP object to use.

    Compare your class to this:

    <?php 
    
    /**
     * checks how many times given IP address has been logged.
     * example usage:
     * <code>
     *  <?php
     *  $mysqli = new mysqli( $host,$user,$pass,$database );
     *  $ip = new IpViews( $mysqli );
     *  $views = $ip->views();
     *  print $views === 1?
     *      "This is the first time we've seen your IP address!":
     *      "We've seen your IP address $views times."
     *  ;
     * </code>
     */
    class IpViews{
    
    // these are protected so you can't mess them up from outside
    protected $ip,$db,$views;
    
    /**
     * sets up object (runs automatically).
     * 
     * @param resource $db              the database connection. 
     *                                  I use mysqli in this example.
     * @param string $ip                the ip address. 
     *                                  I put it second so it is convenient to leave it off -
     *                                  the class will try to use the client's IP in that case.
     * @throws InvalidArgumentException if $db is not an instance of mysqli.
     *                                  if $ip is not a valid ip address.
     */
    public function __construct( $db=null,$ip=null ){
    
        // use client's ip if none provided
        if(  ! $ip ){
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    
        // validate ip address
        if( ! filter_var( $ip,FILTER_VALIDATE_IP ) ){
            throw new InvalidArgumentExcaption( '$ip is not a valid ip address' );
        }
        $this->ip = $ip;
    
        // validate $db connection
        // (you can replace "MySQLi" with whatever DB class you use)
        if( ! $db || ! $db instanceof MySQLi ){
            throw new InvalidArgumentExcaption( '$db is not a usable database connection' );
        }
    
        // increment count in DB and set total number of views
        $this->views = $this->count();
    }
    
    /**
     * selects and updates count from DB.
     * protected so you don't accidentially increment the value from the outside
     *
     * @return int                      number of views for this ip so far; 0 on failure
     */
    protected function count(){
        try{
            // check if the ip already exists in the db
            $SQL_exists = "SELECT `view_count` FROM `ip_table` WHERE `ip`='{$this->ip}'";
            $exists = $this->db->query( $SQL_exists );
            if( ! $exists ){
                throw new BadMethodCallException( "unable to query ip_table: {$this->db->error}" );
            }
    
            // if so, update and return the new count
            if( $exists->num_rows ){
                $SQL_update = "UPDATE `ip_table` SET `view_count` = `view_count`+1 WHERE `ip`='{$this->ip}'";
                if( ! $this->db->query( $SQL_update ) ){
                    throw new BadMethodCallException( "unable to update ip_table: {$this->db->error}" );
                }
                // return the new count
                $row = $exists->fetchRow();
                return $row[0] + 1;
            }
    
            // if not, inset and return 1
            $SQL_insert = "INSERT INTO `ip_table`( `ip`,`view_count` ) VALUES( '{$this->ip}',1 )";
            if( ! $this->db->query( $SQL_insert ) ){
                throw new BadMethodCallException( "unable to insert in ip_table: {$this->db->error}" );
            }
            return 1;
        }
        catch( Exception $e ){
            // you can re-throw the exception, or log it, or whatever
            return 0;
        }
    }
    
    /**
     * gets total number of views on this ip address.
     *
     * @return int                      number of views so far on this ip address.
     */
    public function views(){
        return $this->views;
    }
    }
      18 days later
      cadkins23;11030517 wrote:

      I've been coding for a few years now, but never got the hang of OOP style coding which everyone who is hiring seems to want these days. I have an interview tomorrow and tried to put together a little portfolio today and this is what I came up with:

      www.chrisadkins.me

      Please tell me if you think my OOP style coding is good enough to get a job that is looking for a PHP coder with only 3 years experience. I've been coding with PHP since 2007 but feel like I've never mastered it the way some of you have.

      Thanks for any advice whatsoever!

      I know this is a VERY old post, but ... based on the lack of responses the guy got, I might have some good advice to pass on that relates to this same situation (at least I haven't seen it mentioned anywhere here yet):

      Most job ads et al are written by HR (Human Resources) departments; it's not unusual to find they know little about the actual job and I've never met one that actually had a good understanding of the job being offered.
      That said, simply try to take things like "OOP" with a grain of thought. If you don't know what OOP means for sure, look it up and then craft your resume accordingly.

      As I see it OOP w/r to PHP coding would mean things like:
      1. Using snippets etc, written by others where a good methodology exists.
      2. Using Functions, CSS, HTML 5, Require, Calls, Includes, and on and on: All the things that help by preventing code from having to be typed over and over. Write it once and call for whatever is needed by calling it by whatever the method may be.
      3. A good knowledge of PHP internals, Functions and such, including knowing when/when not to use Globals and so on.

      Mostly, just be as honest and forthcoming as you can and tweak your cover letter and the resume itself to apply specifically to the job you're interested in. Let them know you've researched their company and tell them why you think YOU would be a great candidate for the job you're applying for, if possible.
      Most of all be polite, usassuming and HONEST! Keep the resume and cover letter as short as possible and keep opinions and "feelings" to yourslef - the interview is the place where they'll ask if they want to know about those things.

      I feel qualified to make these statements because I managed to go from Tech to middle managent to upper management (Director of North American and Pac Rim R&D). I hired a few hundred people and sadly had to let a few go, but that's how it all works.

      Luck & Good Luck to anyone who reads this!

      Rivet

        Write a Reply...