Prior to completing the module below it is highly recommended that you look at the following learning material (Note that the content here is PHP 5.4+ specific):
Note: You will stage, commit (with valid message) and push your changes after each update.
- Create a new Git repo in your GitHub account under your namespace called
PHP
. All the code for this module should be committed there. - Create a file called
constants.php
and define 5 PHP constants there via thedefine()
method. Assign some constants string values and some integer values. The integer values should be between one (1) and five (5). - Stage, commit and push your changes.
- Create a new file called
main.php
. In themain.php
file bring in the constants.php file in a way that will make it required and that it will be brought in only one time. - Now create an array variable called
array
and assign it an associative array, using key names that are string constants from your constants.php file and at least one (1) value that is an integer constant from constants.php. - Now create an integer variable called
result
and assign it the output of the multiplication of two (2) or more array values. In this equation you should use array keys that are constants defined inconstants.php
. - Now print out the result as a string in the format of:
"The result of X * Y is: Z"
. Create this message WITHOUT string concatenation and without reassigning any new variables. - Now refactor the printed string to use string concatenation.
- Create a new string variable that holds three (3) multi-line paragraphs of text, this text can be anything you want and should have at least five (5) variable substitutions within the string. You can create the variables to put into this string if necessary. Do not use string concatenation or a multi-line string inside double quotes to create the string's value. Print the string's value.
- Create a new file called
loop-control.php
and inside create an array callednumbers
which contains integer values between0
and100
, with a step of3
between each number (eg.0, 3, 6, 9, 12
). Do this with the least amount of code you can. - Now create a
foreach
loop to iterate over the array, inside theforeach
loop add logic to do the following without using anif-else
statement:- If the number is 3, print
"Three"
one (1) time - If the number is 9, print
"Nine"
three (3) times - If the number is 15, print
"Fifteen"
five (5) times
- If the number is 3, print
- Now refactor the logic inside the
foreach
loop to do the following (if
andelse
can be used now):- If the number is a multiple of
7
print out "Sevens are lucky, this number has X", whereX
is the number multiples of7
represented by the number - Otherwise, if the number is a multiple of
10
, print out"X is a round number"
, whereX
is the number - If the number is the first in the group, print out
"First number"
- If the number is the last in the group, print out
"Last number"
- If the number is a multiple of
- Refactor the code to be a for loop.
- Refactor the code to be a while loop.
- Refactor the code to be a do-while loop.
- Create a new file called
functions.php
. - Create four (4) math functions:
add
,subtract
,multiply
anddivide
two (2) given parameters. - Use all of the functions and print out the result, showing how you would pass arguments to the functions by value and by reference.
- Create a function which takes two (2) comparison parameters and a third boolean parameter defaulting to false, which tells the function whether or not to compare the data type as well. Have the function print out a string that tells if the parameters are equal or not, and if they are equal in datatype if the third parameter was true.
- Use the
comparison
function and pass in the following parameters:4, "4"
5, "5", true
4, 4.0
5. 5.0, true
- Create a new class called Math in a file named
math.php
, and copy your previous four (4) math functions into the class as public functions. - Refactor each function so it will be able to take a minimum of
2
integer parameters with no maximum number of parameters. (eg. You could pass in2
arguments orn
.) The functions should return the relevant operation applied to all the function arguments, in the order they were given. - Create a new file called
do_math.php
, which uses each function in the Math class 3 times and prints out the output. - Now refactor your
Math
class to have 4 constants that represent each math operation and a private method calleddoOperation
whose first parameter is the type of operation to do, and subsequent parameters are the values to do that operation on. Then, refactor the previous functions to all use the newdoOperation
function.
Create a new branch in your PHP Git repository called advanced
and push your code for the following exercises there. Place all the following code in a folder called advanced
.
- Create an inheritance hierarchy composed of six (6) classes:
- Computer > Workstation
- Workstation > Mac
- Workstation > PC
- Computer > Server
- Server > WebServer
- Server > DatabaseServer
- Create a new PHP file called
main.php
which uses these classes. Be creative and show the following concepts (you can simply echo strings for the function logic):- Public, Protected and Private functions
- Public, Protected and Private properties
- Overwriting inherited functions
- Overwriting inherited functions but still executing the parent's functionality for that function before implementing the child's logic
- Refactor your code to show the use of namespaces using the keywords
use
andas
- Now refactor your code to use at least one (1)
interface
- Now refactor your code to include at least one (1)
trait
which should contain a function that executes then extends a super class' functionality - Now refactor your code to include at least three (3)
closures
. If your code already has this, add three (3) more - Stage, commit and push your changes with a commit message of
closures
- Create a new Git Repo in your GitHub namespace called
php-final
and put all your code for the following exercise there. - Create a simple PHP application that does not use an existing framework or 3rd party libraries and has the characteristics listed below.
- Note that this exercise should be kept as simple as possible. Do not develop beyond the feature requirements below.
Before you begin building this web application, take a minute to setup Laravel Homestead if you haven't already.
- Uses the MVC design pattern (limit to one [1] model and one [1] controller for simplicity)
- Uses a namespace
- Saves information into a MySQL database
- Illustrates
create
,read
,update
anddelete
functions on amodel
(all database interaction should happen at the model layer) - Has static methods on the model for
find()
andfindAll()
for fetching model records - Inserts or updates a record into the database using a
save()
method on the model - Deletes the record from the database using a
destroy()
method on the model - Validates properties on the model using a
validate()
function which returnstrue
orfalse
as to whether or not the model is valid - The model's
validate()
function should feed data to anerrors()
function that returns an array of errors (if any) - The model's
save
function should returntrue
orfalse
if ainsert
orupdate
was successful, and should validate the model prior to attempting to save. If the model is invalid, thesave()
should not complete and should returnfalse
. - If a model does not save for some reason, the user should be returned to the
create
orupdate
view and be shown the errors.
When you are finished, push your code to GitHub. You should have two (2) repos, PHP
and php-final
in your personal GitHub namespace. Create a tag called v1.2
with a message of "ready for review"
. Be sure your tags are pushed to the remote repository and are visible in GitHub.
Create an issue titled Review Module 2 - Programming Basics with PHP and @mention
your mentor and team leader.