Lab #5 - PHP Classes
Purpose: Use Classes to implement aspects of a page
Instructions
- This lab will be partially reimplementing lab #3, but with an expansion towards the class model, as well as how we might engage with a database source. If you are not confident in your work from Lab #3, you may use the lab #3 solution as a starting point.
- Create two PHP files: "index.php" and "language.php"
- [language.php] This will only contain the "Language" class you are going to define
- [language.php] require or include the following file in your file: "/var/www/html/resources/csci2006data.php"
- [language.php] In the "Language" class, add the following functions:
- [language.php] __construct function, that takes a single parameter. This parameter is the "key" that identifies which language the class should represent.This function should call the function getCSCI2006Data that I have defined, in order to get the data for this particular language and should store the data in some class attribute(s) in order to reference the data in other functions. "getCSCI2006Data" takes up to two parameters. The first parameter is the "topic" for the dataset, in this case, that topic is "language". The second parameter is the "key" for which language data you want to retrieve. If nothing is provided for the second parameter, a list of key=>name pairs will be returned for all of the available languages
- [language.php] isDefined function, that looks at the data defined for this language instance and returns true if the data represents a language, and false if the data does not represent a language. The "getCSCI2006Data" function returns null if the "key" can not be found in the dataset
- [language.php] getName function that returns the name of the Language from the dataset
- [language.php] toHTML function that converts the language dataset into HTML following the same pattern of HTML from before. I recommend checking out the contents of of the data that is returned from "getCSCI2006Data" using the var_dump function
- [language.php] add a static class function called getAllLanguages that will return a list of key=>name pairs of the languages that are available. Remember that this list can be found by calling the "getCSCI2006Data" with the topic of "language" but with no second parameter.
- [index.php] require or include the language.php file you created
- [index.php] Create a new instance of the Language class using the query string parameter you have been using for your page declaration in the URL for the last few labs
- [index.php] Print out the HTML for the page, including the header and footer HTML. Note, you will need to call the various class methods you defined for language in order to know whether the page is invalid, and you need to print the 404 message; or to get the title of the page (via the language name); or to get the HTML body contents (via the toHTML method); or the list of pages for the navigation bar (via the static method you created -- note the syntax change for calling static methods of classes)
- Your site should look similar to the example site, but might have different extra languages
- Upload your PHP files (if you have not been doing that to test your code as you go), to the remote server. Please put them in the folder "public_html/csci2006/lab05/"
- In a web-browser, go to the URL below
- Ensure that your code is not producing any errors or warnings, by using the log-access tool. If you have trouble understanding the log messages, please email me for assistance. Note the log does not reset, so you need to look at when any errors/warnings occurred and which HTTP request they were for to better understand whether you have already fixed that issue
Submitting Instructions
- Upload a screenshot of your site, on a page of your choosing