Exercise Monday

Goal: Today's goal is to become familiar with using PHP objects and classes to build a basic web app. By the end of the day we'll be familiar with using PHP classes and solving some simple problems with our new skills.

Warm Up

  • Review the data types in PHP with your pair. What are they, and how are they different?
  • Discuss the basic parts of a project in PHP. What are they steps for setting up a PHP project?
  • How are classes declared? What are the benefits of using classes?


Alright, time for you to play with some objects! As a reminder, you don't have to get through all of these projects. Spend time on the ones you do work on, making sure you thoroughly understand them.

Using Git in class

  • As you work on these projects be sure to make frequent Git commits.
  • When you're done, push your work up to a remote Github repository on your account, as well as a separate repository on partner's Github account. Creating small websites to solve problems like these will form a large part of your portfolio at the end of the class, so you should both save your work.
  • Be sure to use detailed commit messages, but keep them straight to the point, try to commit one thing at a time (lots of simple commits are good) and form your message in present tense, as in `git commit -m "Add the foreach loop in the HTML body."
  • Include a README.md file in your repository because that's the first thing that anyone will see in your project.
  • If you find yourself asking if you should make a commit, ask yourself if you would be sad to lose all of the work that you did since your last commit. If the answer is even a little bit "yes", then commit!
  • My rule of thumb is that as soon as I get something working I commit. Then I clean up my work and make another commit.

More Car Dealership

  • Follow along with the car dealership example. Add a constructor to your Car class with default values for some arguments.
  • Instantiate your Car objects using your new constructor. Be sure to try out some different arguments for different cars.
  • Encapsulate your Car class by making all of its properties private and creating getters and setters for each property.
  • Modify the Car class to include a path to a picture of the car, and display it with the other info about the car.
  • Now only show cars that are under a certain price and mileage. Modify the form so that the user can enter a number for each maximum value.
  • Notice that if you enter numbers that are too low on the search form, you will get no matching results to display on your page, and your page will be blank. Add an if statement to your HTML so that if there are no cars to display, a message is shown instead. Hint: You can use the empty function in your condition to check and see if there is anything in an array. Try it out in the PHP shell.
> $array_with_stuff_in_it = array("How", "Are", "You?");
> $blank_array = array();

> if (empty($array_with_stuff_in_it)) {
    echo "This array is empty!";
} else {
    echo "This array is not empty!";
This array is not empty!

> if (empty($blank_array)) {
    echo "This array is empty!";
} else {
    echo "This array is not empty!";
This array is empty!

Your goal today is to get to this point. If you finish early, here are some extra problems for you to work on:


  • Create a Parcel class. Be sure that your Parcel class has a constructor as well as getters and setters for each property. All properties should be set to private. You should be able to create a new parcel and specify the dimensions and weight in the arguments.
  • Create a website where the user can submit a form with the dimensions and weight of a Parcel, and you can use the data in the form to instantiate a Parcel object and display its dimensions and weight.
  • When you call a method called volume() on a Parcel instance, it should return the product of the sides.
  • When you call a costToShip() method on your parcel, return a cost based on a formula you make up.
  • Display the Parcel's cost to ship and the volume with its dimensions in your site.
  • Add validation: Make sure that the Parcel object is not created if any of the form fields are blank. Display an error message instead. Also remember to make sure that your user can only enter numbers into the form.


  • Create a Triangle class.
  • Make a web page that lets the user input the lengths of the sides of a triangle, and tells whether they form an equilateral, isosceles, or scalene triangle. If you're not familiar with these terms, here are definitions:
    • equilateral: all sides are equal;
    • isosceles: exactly 2 sides are equal;
    • scalene: no sides are equal.

Keep in mind that not all combinations of three numbers make valid triangles. For example, (2, 2, 8) does not make a triangle. The rule is that if any side is at least as long as the other two combined, the lengths do not form a triangle.

Peer Code Review

  • Is the project set up correctly?
  • Can we use the Dealership website correctly? Take a look at the code, does it make sense?
  • How might a programmer go about DRYing up this code?