Object Oriented PHP page counter?
-
For my php class, I am supposed to take a simple bit of code that the teacher created and turn it into an object oriented page counter. However, my teacher is poor at explaining objects in general, and I could really use some help The perimeters of this assignment are: -Create a page hit counter object that uses files to store the visit number and an to store details about the visit. -Name the object "PageCounter" and place the code into a file named "PageCounter.php" -When you create the new object pass the creator the "Page name" -add a method called count() that will open the file "PAGENAME.count" read in the number increment it and write it back out. -also have count add a line to "PAGENAME.log" that contains the date, time, $_SERVER["REMOTE_ADDR"],, and $_SERVER["REMOTE_HOST"] as a CSV so that you can see who has visited the site in detail. -add a method called show() that will output a pretty <p></p> with the current page count. And this is the code he provided: <?php $c = fopen("control.txt","r"); $num = fgets($c); $num+=1; fclose($c); $c = fopen("control.txt","w"); fputs($c, $num); fclose($c); ?> Any and all assistance would be adored. Thank you all for your time.
-
Answer:
Lots of info...here we go. First of all, an object is really just a summation of properties and functions. Simple example: a lamp. A lamp can be broken down into simple pieces: a bulb (what power rating) and "status" (is it on or off) - these are the "properties" that make up a lamp. Now, you can also perform some actions (functions) on the lamp: "turn it on", "turn it off" and "change the bulb". When you create an "object" in Object-Oriented Programming, all you are doing is breaking it down into these parts - what is is made of and what can you do with it. Next is what is called "instantiating" an object - this is where you create a new "instance" of your class object. Think about your house. You have multiple lamps. Each lamp in your house is a separate instance of the generic "lamp" object. So you can turn one on while a different one is turned off. And you can change the bulb in one without changing the bulb in another. The important thing to remember is that when you call a class function, the action is taken on the given instance of the class object - not on the class itself. On to your case. You are trying to create an object "PageCounter" stored in "PageCounter.php". Your teacher hasn't explicitly told you which properties to give to your class, but the function list is fairly clear. Here is how I would break it down. In case you are not familiar with UML or something similar, the list below basically shows variables (properties) in the first block and functions in the second block. Technically, "-" designates a private element and "+" designates a public element, but you probably don't need to get this involved yet. Page Count Object: ============== - $pageName: variable to store the name of the page (for this instance only) - $countFile: variable to store the path to the applicable counter file - just for simplicity - $logFile: variable to store the path to your log file - $pageCount: variable to store the page count last read from the text file ============== + PageCounter(): main constructor (gets called when you create a new instance of PageCounter) - to which you will pass the "Page Name". Store that name in the variable $pageName and populate the $countFile variable with the name of the corresponding text file which will store your counter. If the counter file already exists, read the data and store the count in $pageCount - otherwise set the page counter to 0. + Count(): when this function gets called, you will open the $countFile for reading, read the counter, increment it and store the result in $pageCount - then write $pageCount back to the $countFile. Then open your $logFile for writing and append the $_SERVER variables as instructed above. + Show(): when this function is called, you will just return an HTML string with your formatted page count - something like "<p>" . $this->pageCount . "</p>". I don't want to take away the fun of conquering this project yourself, so I'll leave you to write your PageCount class. But to get you started, here is a "lamp" class based on what we discussed above: lamp.php ======= class Lamp{ private $bulbWattage; private $status; function __construct($power){ // create a new lamp and set the power rating of its bulb $this->bulbWattage = $power; $this->status = false; // not turned on by default } function TurnOn(){ $this->status = true; } function TurnOff(){ $this->status = false; } function ChangeBulb($power){ $this->bulbWattage = $power; } function GetStatus(){ if ($this->status){ return "<p>Lamp is on: " . $this->bulbWattage . " W bulb</p>"; } else{ return "<p>Lamp is turned off</p>"; } } }; Now you can instantiate your lamp class from your main PHP file (be sure to include "lamp.php"). Notice below we create two instances of the Lamp class. Turn only one of them on and then get the status of each. In this example, the first lamp would return the "on" message and the second would return the "off" message. $myLamp1 = new Lamp(60); $myLamp1->TurnOn(); $myLamp2 = new Lamp(100); echo $myLamp1->GetStatus(); echo $myLamp2->GetStatus(); Hopefully this helps! Good luck.
shawneef... at Yahoo! Answers Visit the source
Related Q & A:
- Is Java fully object-oriented?Best solution by Stack Overflow
- What are the object-oriented features of Visual Basic.NET?Best solution by msdn.microsoft.com
- How to Convert a Procedural Programming into Object-Oriented Programming?Best solution by Stack Overflow
- What's wrong with my yahoo 360 page stat counter?Best solution by answers.yahoo.com
- How to make a php article counter?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.