Skip to content

Instantly share code, notes, and snippets.

@SinisterMinister
Last active December 18, 2015 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SinisterMinister/5758282 to your computer and use it in GitHub Desktop.
Save SinisterMinister/5758282 to your computer and use it in GitHub Desktop.
Marty's Homework

Marty's Homework

Classes and Objects: Lesson 1

Instructions

Make answers to the following problems below using PHP. For now, you can psuedocode the methods with comments, or you can create your own implementations if you feel adventurous. Use any resources you like. Also, one thing I forgot to mention in your lesson: there is a naming convention for things in classes. If a name starts with a single underscore, it's likely to be private or protected. For this example, any names with a leading underscore, mark it as protected. If you have any questions, feel free to ask.

Example of pseudo-coded method

class Foo {
    public function bar($data) {
        /* Make sure to put it into comments, otherwise your editor with flip its shit.
        if $data is array
            foreach $item in $data
                do_stuff($item)
        else
            return false
        */
    }
}

Problems

1. Build a User class with the following properties and methods:

Properties
  • id
  • first_name
  • last_name
  • user_name
  • email
  • password
  • last_logged_in
  • active
Methods
  • save() - Saves the user to some sort of persistant storage (database, file, etc)
  • load() - Load the user from persistant storage
  • drop() - Delete the user from persistant storage
  • get_full_name() - Gets the user's full name with the format "{first_name} {last_name}"
  • get_comments() - Gets the user's comments

2. Build a Blog_Post class with the following properties and methods:

Properties
  • id
  • poster_id
  • date_created
  • date_published
  • last_edited
  • content
  • _published - Defaults to FALSE
Methods
  • save()
  • load()
  • drop()
  • publish()
  • is_published()
  • get_comments()

3. Design the Comment class with the following specs:

Comments

  • are made by Users
  • are added to Blog_Posts
  • have content
  • have a day they were created
  • have a score based on up and down votes
  • must be saved
  • must be loadable
  • can be deleted
  • can be upvoted and downvoted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment